新闻详情

新闻详情

首页 / 资讯中心 / 详情

Crawlee 如何用 SitemapRequestLoader 从 sitemap 批量读取 URL 启动全站爬取

发布时间:2026/9/13 23:58:17来源:尧图网络
Crawlee 如何用 SitemapRequestLoader 从 sitemap 批量读取 URL 启动全站爬取
Crawlee 如何用 SitemapRequestLoader 从 sitemap 批量读取 URL 启动全站爬取【免费下载链接】crawleeCrawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.项目地址: https://gitcode.com/GitHub_Trending/cr/crawlee当你拿到一个网站的 sitemapXML 或纯文本格式遵循 Sitemaps protocol和 crawl sitemap 示例给出从单独验证加载、到接入 Crawler 发起全站爬取的完整操作路径。有一个边界需要先明确SitemapRequestLoader只支持遵循标准 Sitemaps 协议的 XML 和纯文本 sitemap。包含链接的 HTML 页面不在支持范围内——这类页面应交给 Crawler 的enqueueLinks功能处理。准备条件Node.js 16 或更高版本见 README.md。安装crawleenpm 包npm install crawlee目标站点提供可访问的 sitemap URL如https://example.com/sitemap.xml。下文示例沿用文档中的https://crawlee.dev/sitemap.xml替换为你自己的站点即可。原理只读加载器不能直接交给 CrawlerCrawlee 的 Crawler 从单个IRequestManager读取请求通过requestManager选项传入。而SitemapRequestLoader实现的是只读的IRequestLoader接口不允许新增或重试请求因此不能直接传给 Crawler。正确的组合方式是RequestManagerTandem把只读的SitemapRequestLoader和可写的RequestQueue拼在一起。其工作机制是——只要 sitemap 加载器还有未处理的请求就先把请求转入RequestQueue再交给 Crawler 处理爬取过程中动态发现的新请求和失败重试则直接进队列一侧。由于每个请求都会经过队列去重和重试得到统一处理同一 URL 不会被重复爬取。第一步手动迭代验证 sitemap 能被批量读出在接入 Crawler 之前先用文档中的基本用法确认 sitemap 可以正常加载import { SitemapRequestLoader } from crawlee; // Open a sitemap request list. The sitemap is fetched and parsed in the background, // so crawling can start before the whole sitemap is loaded. const sitemapRequestLoader await SitemapRequestLoader.open({ sitemapUrls: [https://crawlee.dev/sitemap.xml], // Optionally filter the URLs read from the sitemap: // include: [https://crawlee.dev/docs/**], }); for await (const request of sitemapRequestLoader) { console.log(request.url); await sitemapRequestLoader.markRequestAsHandled(request); }运行这段脚本控制台会逐条打印 sitemap 中的 URL。两个行为值得注意SitemapRequestLoader.open()解析完成并不表示 sitemap 已读完——加载在后台进行open()可能先于解析完成返回。需要确认加载进度时用isSitemapFullyLoaded()检查见 实现源码。for await循环持续到 sitemap 全部加载完毕且所有 URL 都已被markRequestAsHandled消费后才结束因此脚本正常退出即说明整个 sitemap 被完整读出。第二步接入 Crawler启动全站爬取验证加载器可用后用toTandem()辅助方法把加载器和默认RequestQueue组成 tandem再传给 Crawlerimport { CheerioCrawler, SitemapRequestLoader } from crawlee; // Read the initial URLs from a sitemap. const sitemapRequestLoader await SitemapRequestLoader.open({ sitemapUrls: [https://crawlee.dev/sitemap.xml], }); // Pair the loader with the default RequestQueue via the toTandem() shortcut. const requestManager await sitemapRequestLoader.toTandem(); const crawler new CheerioCrawler({ requestManager, async requestHandler({ enqueueLinks }) { await enqueueLinks(); }, }); await crawler.run();sitemapUrls指向站点入口 sitemap 后爬取过程是sitemap 中的 URL 先被排入队列并被处理requestHandler里的enqueueLinks()会把每个页面上发现的链接追加进队列实现从 sitemap 出发、逐页扩展的全站爬取。可选分支显式指定 RequestQueue如果你想在爬取前对队列做自己的配置比如指定队列名称可以不依赖toTandem()的默认队列改用显式写法import { CheerioCrawler, RequestManagerTandem, RequestQueue, SitemapRequestLoader } from crawlee; // Read the initial URLs from a sitemap. const sitemapRequestLoader await SitemapRequestLoader.open({ sitemapUrls: [https://crawlee.dev/sitemap.xml], }); // A writable queue for requests discovered during the crawl. const requestQueue await RequestQueue.open(); const requestManager new RequestManagerTandem(sitemapRequestLoader, requestQueue); const crawler new CheerioCrawler({ requestManager, async requestHandler({ enqueueLinks }) { await enqueueLinks(); }, }); await crawler.run();两种写法的差别仅在于队列的创建方式爬取行为一致。控制读取哪些 URLinclude / exclude 与 enqueueStrategySitemapRequestLoader.open()支持三类 URL 过滤完整选项定义见 packages/core/src/storages/sitemap_request_loader.tsinclude/excludeURL 模式数组支持 glob 字符串、{ glob: string }对象、RegExp实例或{ regexp: RegExp }对象。glob 匹配始终不区分大小写需要区分大小写时使用RegExp。enqueueStrategy保留相对父 sitemap URL 符合该策略的 URL非http(s)scheme 的条目一律丢弃传all可关闭主机过滤。默认值为EnqueueStrategy.SameHostname即默认只保留与 sitemap 同主机的 URL。const sitemapRequestLoader await SitemapRequestLoader.open({ sitemapUrls: [https://crawlee.dev/sitemap.xml], include: [https://crawlee.dev/docs/**], });上例中include写法来自 request_loaders 指南的代码注释示例表示只读取/docs下的页面。结果验证crawler.run()正常返回且进程退出说明队列中的请求已全部处理完毕。请求队列的数据落在本地磁盘的CRAWLEE_STORAGE_DIR环境变量指向的目录未设置时默认为当前工作目录下的./storage。默认请求队列的文件路径为{CRAWLEE_STORAGE_DIR}/request_queues/default/entries.json见 Request storage 指南——打开该文件确认其中包含 sitemap 派生出的 URL即可核对爬取范围符合预期。爬取中需要确认后台加载是否读完 sitemap 时调用加载器的isSitemapFullyLoaded()。限制与替代路径再次强调格式边界只支持 Sitemaps 协议的 XML / 纯文本 sitemapHTML 链接页请改用 Crawler 的enqueueLinks。open()先于后台解析完成返回不要以open()的 resolve 作为“sitemap 已全部读完”的判断依据。如果你的需求只是把 sitemap 解析成 URL 列表再交给 CrawlerCrawl a sitemap 示例给出了另一条路径用crawlee/utils的Sitemap工具类加载后批量入队——import { CheerioCrawler, Sitemap } from crawlee; const crawler new CheerioCrawler({ async requestHandler({ request, log }) { log.info(request.url); }, }); const { urls } await Sitemap.load(https://crawlee.dev/sitemap.xml); await crawler.addRequests(urls); await crawler.run();该方式会先完整取得 URL 列表再启动爬取适合 URL 规模不大、希望一次性入队的场景而SitemapRequestLoader的后台流式加载更适合大型 sitemap。【免费下载链接】crawleeCrawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.项目地址: https://gitcode.com/GitHub_Trending/cr/crawlee创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
网站建设高端定制企业官网
RELATED

相关资讯

更多精彩内容,欢迎继续阅读

较早相关资讯

最新相关资讯

Python运维相关的笔试题及答案 2026/9/14 0:52:22

Python运维相关的笔试题及答案

笔试题及答案项目代码本文档是一套笔试题库, 其中包含详细答案, 题型包含选择题, 解答题以及编程题, 全面覆盖了基础知识点。2023年《网络建设与运维》国赛脚本文件及导出答案视频需要参赛的人员要对最少一种脚本语言做到熟悉, 并且能够领会脚本里和网络有关的指令, 从而迅速地…

阅读更多 →
示波器八大灵魂问题:接地、触发、带宽与采样率深度解析 2026/9/14 0:49:22

示波器八大灵魂问题:接地、触发、带宽与采样率深度解析

1. 这不是说明书,是八个真正能让你“看懂波形”的灵魂拷问示波器不是万用表,它不告诉你“电压是多少”,而是告诉你“电压是怎么变的”。很多人买了示波器,接上探头,屏幕亮了,波形跳了,然后——就…

阅读更多 →
高分辨率示波器如何提升信号完整性分析能力 2026/9/14 0:49:22

高分辨率示波器如何提升信号完整性分析能力

1. 这不是普通示波器,而是工程师口袋里的“信号显微镜”你有没有遇到过这样的场景:调试一个开关电源,纹波看起来不大,但系统偏偏在特定负载下偶发重启;或者排查一段高速SPI通信,逻辑分析仪显示时序“完全正…

阅读更多 →
中小企业 AI 落地平台评测:4 个真实案例 2026/9/14 0:49:22

中小企业 AI 落地平台评测:4 个真实案例

中小企业 AI 落地平台评测:4 个真实案例⚠️ 本文客户案例均做脱敏说明,不指代具体客户。“中小企业 AI 落地平台哪家好?”——这是 5-50 人中小企业老板最常问的问题。 但"好不好"是相对的。有人觉得好用,有人觉得一般…

阅读更多 →
工业 AI 落地:3 个工厂的真实路径 2026/9/14 0:49:22

工业 AI 落地:3 个工厂的真实路径

工业 AI 落地:3 个工厂的真实路径⚠️ 本文客户案例均做脱敏说明,不指代具体客户。“工业 AI 落地”——这是 2026 年制造业老板最关心的话题,没有之一。 但"工业 AI"是个特别容易被夸大的词。真正的工业 AI,不是"…

阅读更多 →
智能体可视化设计用哪家:3 类工具对比 2026/9/14 0:49:22

智能体可视化设计用哪家:3 类工具对比

智能体可视化设计用哪家:3 类工具对比⚠️ 以下对比基于公开资料整理,不构成选型建议。“智能体可视化设计用哪家?”——这是 2026 年中小企业老板 产品经理最常问的问题之一。 "智能体"和"可视化设计"这两个词都很热&a…

阅读更多 →

今日资讯

本周资讯

本月资讯

看完文章仍有疑问?

联系尧图顾问,获取一对一建站咨询

立即免费咨询 📞 400-888-8888
📞