ARTICLE DETAIL

资讯详情

深耕网站视觉设计与运营推广的一线实战洞察。

AsyncWebCrawler.arun() - Memory Context

AsyncWebCrawler.arun() - Memory Context AsyncWebCrawler.arun() - Memory Context【免费下载链接】crawl4ai Crawl4AI: Open-source LLM Friendly Web Crawler Scraper. Dont be shy, join here: https://discord.gg/jP8KfhDhyN项目地址: https://gitcode.com/GitHub_Trending/craw/crawl4aiSignatureasync def arun( url: str, config: CrawlerConfig None, session_id: str None, **kwargs ) - CrawlResultParametersurl: Target URL to crawlconfig: Optional configuration objectsession_id: Optional session identifier for caching ...**与当前源码对照**[async_webcrawler.py](https://link.gitcode.com/i/fc7a3003ca9e19b580dd392e006e9a86) 中 arun 的实际签名是 async def arun(self, url: str, config: CrawlerRunConfig None, **kwargs) - CrawlResultContainerdocstring 中还保留了从kwargs 散参到 CrawlerRunConfig 配置对象的迁移指南。可见博客中的签名是教学示意当前版本以 CrawlerRunConfig 承载运行期配置、返回代理单结果的 CrawlResultContainer保持 result.markdown、result.html 等属性访问的向后兼容。这本身就是一个 Memory 上下文的典型演进API 事实会随版本变化而三维协议关心的是*持续维护这类事实*的机制。 在仓库的实际 Memory 文件里这种精确、无歧义的参考风格被贯彻得很彻底。例如 [simple_crawling.txt](https://link.gitcode.com/i/d6d6053e9eca637099d096b174872a34) 直接给出了最小可运行骨架、CrawlResult 的字段访问方式result.markdown.raw_markdown / fit_markdown、result.media、result.links以及错误处理模式 python async with AsyncWebCrawler(configbrowser_config) as crawler: result await crawler.arun(urlhttps://example.com, configrun_config) print(result.markdown)以及配置参数的精确语义word_count_threshold为块级最小词数、excluded_tags跳过指定标签、process_iframes是否处理 iframe 内容等。3.2 Reasoning灵魂The SoulReasoning 承载的是库作者的设计哲学——不只是库做什么更是为什么这样做。原文坦承这是最难写的部分因为它需要真正的理解一个库如果缺少这部分是个危险信号。原文给出的示例包括为什么选择异步优先架构网络 I/O 慢同步等待浪费约 90% 的执行时间现代站点资源并行加载你很少只抓一页。何时使用会话管理不只是性能问题更是表现得更像人——抓同一域名多页时用会话、复用浏览器上下文保留 cookie 与本地存储但会话过长反而可疑。缓存策略决策树if static_content and infrequent_updates: use_cache_mode(read_write) elif dynamic_content and real_time_needed: use_cache_mode(bypass) else: use_cache_mode(read_only) # Safe default仓库中的 Reasoning 文件用Mermaid 工作流图来承载这种设计原理可视化。例如 diagrams/simple_crawling.txt 开头的时序图把创建 AsyncWebCrawler → 启动浏览器 → arun(url, config) → 抽取 HTML → 清洗 → 生成 Markdown → 抽取媒体/链接 → 返回 CrawlResult的异步流程画成sequenceDiagram配置流程图则用flowchart展示了BrowserConfig/CrawlerRunConfig缺省回退、excluded_tags/css_selector/markdown_generator/process_iframes各开关在管线中的判定顺序。diagrams/deep_crawling.txt 则用流程图对比了 BFS/DFS/Best-First 三种深度爬取策略的取舍——这正是设计决策 权衡这一 Reasoning 维度的典型形态。3.3 Examples实践The Practice第三支柱是纯粹的代码模式没有废话只有模式在跑。原文给出的三类示例# 带 JavaScript 执行的抓取 result await crawler.arun( urlhttps://example.com, js_codewindow.scrollTo(0, document.body.scrollHeight);, wait_forcss:.lazy-loaded-content ) # 用 CSS 选择器抽取结构化数据 result await crawler.arun( urlhttps://shop.example.com, extraction_strategyCSSExtractionStrategy({ prices: span.price::text, titles: h2.product-title::text }) ) # 基于会话的抓取自定义请求头场景 async with crawler: result1 await crawler.arun(url1, session_idproduct_scan) result2 await crawler.arun(url2, session_idproduct_scan)【免费下载链接】crawl4ai Crawl4AI: Open-source LLM Friendly Web Crawler Scraper. Dont be shy, join here: https://discord.gg/jP8KfhDhyN项目地址: https://gitcode.com/GitHub_Trending/craw/crawl4ai创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表