[Bug]: BestFirstCrawlingStrategy fetches a full batch past max_pages

未关闭 适合新手
#2,381 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
74/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
data

调研方向

从 BestFirstCrawlingStrategy 的 _arun_best_first 方法开始,看其中填充每个批次的循环。它计算了 batch_size = min(BATCH_SIZE, remaining),但迭代的是 range(BATCH_SIZE),因此循环应使用 batch_size。使用 max_pages=12 运行复现脚本,确认只抓取 12 个页面。当抓取数量与 max_pages 一致且现有的深度爬取测试仍然通过时即完成。未合并的 PR #2246 使用了相同的模式,请检查它是否存在冲突。

由索引模型根据 Issue 内容生成。

描述

⚙ Done 🐞 Bug
crawl4ai version

0.9.5 (same on 0.9.0)

Expected Behavior

BestFirstCrawlingStrategy with max_pages=N fetches about N pages: the last batch is cut to the remaining budget.

Current Behavior

_arun_best_first computes batch_size = min(BATCH_SIZE, remaining) but pops range(BATCH_SIZE) items, so the last batch is always 10 URLs. They are all fetched, and the results past the limit are dropped. With max_pages=12, 21 pages are fetched for 12 results (24 on 0.9.0).

Is this reproducible?

Yes

Code snippets
import asyncio

from aiohttp import web
from crawl4ai import AsyncWebCrawler, CrawlerRunConfig
from crawl4ai.deep_crawling import BestFirstCrawlingStrategy

hits = []


async def handle(request):
    hits.append(request.path)
    links = "".join(f'<a href="/p{i}">p{i}</a>' for i in range(30)) if request.path == "/" else ""
    return web.Response(text=f"<html><body>{links}<p>{'text ' * 200}</p></body></html>", content_type="text/html")


async def main():
    app = web.Application()
    app.router.add_get("/{tail:.*}", handle)
    runner = web.AppRunner(app)
    await runner.setup()
    await web.TCPSite(runner, "127.0.0.1", 8766).start()
    config = CrawlerRunConfig(deep_crawl_strategy=BestFirstCrawlingStrategy(max_depth=1, max_pages=12), stream=True)
    async with AsyncWebCrawler() as crawler:
        results = [r async for r in await crawler.arun("http://127.0.0.1:8766/", config=config)]
    print("results:", len(results), "pages fetched:", len([h for h in hits if h != "/robots.txt"]))  # 12 vs 21
    await runner.cleanup()


asyncio.run(main())
Suggested fix

for _ in range(batch_size): in the batch-filling loop. The open PR #2246 (configurable batch_size) keeps the same pattern: it computes effective_batch_size but loops over range(self.batch_size).

主要语言
Python
星标
86.4k
派生
9k
平均合并
2 天 12 小时
30 天内合并 PR
21

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

unclecode/crawl4ai 的其他 Issue

查看 unclecode/crawl4ai 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。