Note
本文档由 WeHub 基于上游 README 翻译整理,属于社区翻译,非官方中文文档。
English · 原始项目 · 上游 README
原作者、版权与许可证归属以原始项目及本仓库 LICENSE 文件为准。
Crawlee 端到端覆盖你的爬取与抓取需求,助你快速构建可靠的抓取器。
即使使用默认配置,你的爬虫也能表现得像真人一样,并躲过现代机器人防护的检测。Crawlee 提供爬取网页链接、抓取数据并将其存储到磁盘或云端的工具,同时保持可配置,以满足你项目的需求。
Crawlee 以 crawlee NPM 包的形式提供。
👉 在 Crawlee 项目网站 查看完整文档、指南与示例** 👈
你更喜欢 🐍 Python 而非 JavaScript?👉 查看 Crawlee for Python 👈.
我们建议查看 Crawlee 文档中的 入门教程 以了解更多信息。
Crawlee 需要 Node.js 16 或更高版本。
试用 Crawlee 最快的方式是使用 Crawlee CLI 并选择 Getting started example 示例。CLI 会安装所有必要依赖,并添加样板代码供你上手体验。
npx crawlee create my-crawler
cd my-crawler
npm start
如果你更希望将 Crawlee 添加到你自己的项目中,请尝试下面的示例。由于它使用了 PlaywrightCrawler,我们还需要安装 Playwright.。为减小安装体积,它并未与 Crawlee 捆绑在一起。
npm install crawlee playwright
import { PlaywrightCrawler, Dataset } from 'crawlee';
// PlaywrightCrawler crawls the web using a headless
// browser controlled by the Playwright library.
const crawler = new PlaywrightCrawler({
// Use the requestHandler to process each of the crawled pages.
async requestHandler({ request, page, enqueueLinks, log }) {
const title = await page.title();
log.info(`Title of ${request.loadedUrl} is '${title}'`);
// Save results as JSON to ./storage/datasets/default
await Dataset.pushData({ title, url: request.loadedUrl });
// Extract links from the current page
// and add them to the crawling queue.
await enqueueLinks();
},
// Uncomment this option to see the browser window.
// headless: false,
});
// Add first URL to the queue and start the crawl.
await crawler.run(['https://crawlee.dev']);
默认情况下,Crawlee 会将数据存储到当前工作目录下的 ./storage。你可以通过 Crawlee 配置覆盖该目录。详情请参阅 配置指南,、请求存储 和 结果存储.
我们会为 Crawlee 中每一次合并的代码变更提供自动化的 beta 构建。你可以在 npm 发布列表. 中找到它们。如果你想在我们正式发布之前测试新功能或 bug 修复,可以像这样安装 beta 构建:
npm install crawlee@next
如果你还使用 Apify SDK,,你需要在 package.json 文件中指定依赖覆盖(dependency overrides),以免安装多个版本的 Crawlee:
{
"overrides": {
"apify": {
"@crawlee/core": "$crawlee",
"@crawlee/types": "$crawlee",
"@crawlee/utils": "$crawlee"
}
}
}
Crawlee 是开源的,可在任何地方运行,但由于它由 Apify, 开发,因此在 Apify 平台上搭建并在云端运行十分便捷。访问 Apify SDK 网站 了解更多关于将 Crawlee 部署到 Apify 平台的信息。
如果你发现 Crawlee 的任何 bug 或问题,请 在 GitHub 提交 issue.。如有疑问,你可以在 Stack Overflow,、GitHub Discussions 提问,或加入我们的 Discord 服务器.
欢迎你的代码贡献,你将获得永世的赞誉!如果你有任何改进想法,请提交 issue 或创建 pull request。贡献指南与行为准则请参阅 CONTRIBUTING.md.
本项目采用 Apache License 2.0 许可 — 详情请参阅 LICENSE.md 文件。