ARTICLE DETAIL

资讯详情

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

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

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),仅供参考
返回列表