160 lines
7.8 KiB
Markdown
160 lines
7.8 KiB
Markdown
<!-- WEHUB_ZH_README -->
|
||
> [!NOTE]
|
||
> 本文档由 WeHub 基于上游 README 翻译整理,属于社区翻译,非官方中文文档。
|
||
> [English](./README.en.md) · [原始项目](https://github.com/apify/crawlee) · [上游 README](https://github.com/apify/crawlee/blob/HEAD/README.md)
|
||
> 原作者、版权与许可证归属以原始项目及本仓库 LICENSE 文件为准。
|
||
|
||
<h1 align="center">
|
||
<a href="https://crawlee.dev">
|
||
<picture>
|
||
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/apify/crawlee/master/website/static/img/crawlee-dark.svg?sanitize=true">
|
||
<img alt="Crawlee" src="https://raw.githubusercontent.com/apify/crawlee/master/website/static/img/crawlee-light.svg?sanitize=true" width="500">
|
||
</picture>
|
||
</a>
|
||
<br>
|
||
<small>用于网页抓取与浏览器自动化的库</small>
|
||
</h1>
|
||
|
||
<p align=center>
|
||
<a href="https://trendshift.io/repositories/5179" target="_blank"><img src="https://trendshift.io/api/badge/repositories/5179" alt="apify%2Fcrawlee | Trendshift" style="width: 250px; height: 55px;" width="250" height="55"/></a>
|
||
</p>
|
||
|
||
<p align=center>
|
||
<a href="https://www.npmjs.com/package/@crawlee/core" rel="nofollow"><img src="https://img.shields.io/npm/v/@crawlee/core.svg" alt="NPM latest version" data-canonical-src="https://img.shields.io/npm/v/@crawlee/core/next.svg" style="max-width: 100%;"></a>
|
||
<a href="https://www.npmjs.com/package/@crawlee/core" rel="nofollow"><img src="https://img.shields.io/npm/dm/@crawlee/core.svg" alt="Downloads" data-canonical-src="https://img.shields.io/npm/dm/@crawlee/core.svg" style="max-width: 100%;"></a>
|
||
<a href="https://discord.gg/jyEM2PRvMU" rel="nofollow"><img src="https://img.shields.io/discord/801163717915574323?label=discord" alt="Chat on discord" data-canonical-src="https://img.shields.io/discord/801163717915574323?label=discord" style="max-width: 100%;"></a>
|
||
<a href="https://github.com/apify/crawlee/actions/workflows/test-ci.yml"><img src="https://github.com/apify/crawlee/actions/workflows/test-ci.yml/badge.svg?branch=master" alt="Build Status" style="max-width: 100%;"></a>
|
||
</p>
|
||
|
||
Crawlee 端到端覆盖你的爬取与抓取需求,**助你快速构建可靠的抓取器。**
|
||
|
||
即使使用默认配置,你的爬虫也能表现得像真人一样,并躲过现代机器人防护的检测。Crawlee 提供爬取网页链接、抓取数据并将其存储到磁盘或云端的工具,同时保持可配置,以满足你项目的需求。
|
||
|
||
Crawlee 以 [`crawlee`](https://www.npmjs.com/package/crawlee) NPM 包的形式提供。
|
||
|
||
> 👉 **在 [Crawlee 项目网站](https://crawlee.dev)** 查看完整文档、指南与示例** 👈
|
||
|
||
> 你更喜欢 🐍 Python 而非 JavaScript?[👉 查看 Crawlee for Python 👈](https://github.com/apify/crawlee-python).
|
||
|
||
## 安装
|
||
|
||
我们建议查看 Crawlee 文档中的 [入门教程](https://crawlee.dev/js/docs/introduction) 以了解更多信息。
|
||
|
||
> Crawlee 需要 **Node.js 16 或更高版本**。
|
||
|
||
### 使用 Crawlee CLI
|
||
|
||
试用 Crawlee 最快的方式是使用 **Crawlee CLI** 并选择 **Getting started example** 示例。CLI 会安装所有必要依赖,并添加样板代码供你上手体验。
|
||
|
||
```bash
|
||
npx crawlee create my-crawler
|
||
```
|
||
|
||
```bash
|
||
cd my-crawler
|
||
npm start
|
||
```
|
||
|
||
### 手动安装
|
||
如果你更希望将 Crawlee **添加到你自己的项目**中,请尝试下面的示例。由于它使用了 `PlaywrightCrawler`,我们还需要安装 [Playwright](https://playwright.dev).。为减小安装体积,它并未与 Crawlee 捆绑在一起。
|
||
|
||
```bash
|
||
npm install crawlee playwright
|
||
```
|
||
|
||
```js
|
||
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 配置覆盖该目录。详情请参阅 [配置指南](https://crawlee.dev/js/docs/guides/configuration),、[请求存储](https://crawlee.dev/js/docs/guides/request-storage) 和 [结果存储](https://crawlee.dev/js/docs/guides/result-storage).
|
||
|
||
### 安装预发布版本
|
||
|
||
我们会为 Crawlee 中每一次合并的代码变更提供自动化的 beta 构建。你可以在 npm [发布列表](https://www.npmjs.com/package/crawlee?activeTab=versions). 中找到它们。如果你想在我们正式发布之前测试新功能或 bug 修复,可以像这样安装 beta 构建:
|
||
|
||
```bash
|
||
npm install crawlee@next
|
||
```
|
||
|
||
如果你还使用 [Apify SDK](https://github.com/apify/apify-sdk-js),,你需要在 `package.json` 文件中指定依赖覆盖(dependency overrides),以免安装多个版本的 Crawlee:
|
||
|
||
```json
|
||
{
|
||
"overrides": {
|
||
"apify": {
|
||
"@crawlee/core": "$crawlee",
|
||
"@crawlee/types": "$crawlee",
|
||
"@crawlee/utils": "$crawlee"
|
||
}
|
||
}
|
||
}
|
||
```
|
||
|
||
## 🛠 特性
|
||
|
||
- 用于 **HTTP 与无头浏览器(headless browser)** 爬取的统一接口
|
||
- 持久化 **队列(queue)**,用于管理待爬取 URL(支持广度优先与深度优先)
|
||
- 可插拔的 **存储(storage)**,同时支持表格数据与文件
|
||
- 根据可用系统资源自动 **扩缩(scaling)**
|
||
- 内置 **代理轮换(proxy rotation)** 与会话管理
|
||
- 可通过 **钩子(hooks)** 自定义生命周期
|
||
- 用于初始化项目的 **CLI**
|
||
- 可配置的 **路由(routing)**、**错误处理(error handling)** 与 **重试(retries)**
|
||
- 可直接部署的 **Dockerfiles**
|
||
- 使用 **TypeScript** 编写,支持泛型(generics)
|
||
|
||
### 👾 HTTP 爬取
|
||
|
||
- 零配置的 **HTTP2** 支持,代理同样适用
|
||
- 自动生成**类浏览器请求头**
|
||
- 复现浏览器的 **TLS 指纹**
|
||
- 集成高速 **HTML 解析器**:Cheerio 与 JSDOM
|
||
- 当然,你也可以抓取 **JSON API**
|
||
|
||
### 💻 真实浏览器爬取
|
||
|
||
- JavaScript **渲染(rendering)** 与 **截图(screenshots)**
|
||
- 支持 **无头(headless)** 与 **有头(headful)** 模式
|
||
- 零配置生成**类人指纹**
|
||
- 自动 **浏览器管理**
|
||
- 使用同一接口调用 **Playwright** 与 **Puppeteer**
|
||
- 支持 **Chrome**、**Firefox**、**WebKit** 等
|
||
|
||
## 在 Apify 平台上使用
|
||
|
||
Crawlee 是开源的,可在任何地方运行,但由于它由 [Apify](https://apify.com), 开发,因此在 Apify 平台上搭建并在云端运行十分便捷。访问 [Apify SDK 网站](https://sdk.apify.com) 了解更多关于将 Crawlee 部署到 Apify 平台的信息。
|
||
|
||
## 支持
|
||
|
||
如果你发现 Crawlee 的任何 bug 或问题,请 [在 GitHub 提交 issue](https://github.com/apify/crawlee/issues).。如有疑问,你可以在 [Stack Overflow](https://stackoverflow.com/questions/tagged/apify),、GitHub Discussions 提问,或加入我们的 [Discord 服务器](https://discord.com/invite/jyEM2PRvMU).
|
||
|
||
## 贡献
|
||
|
||
欢迎你的代码贡献,你将获得永世的赞誉!如果你有任何改进想法,请提交 issue 或创建 pull request。贡献指南与行为准则请参阅 [CONTRIBUTING.md](https://github.com/apify/crawlee/blob/master/CONTRIBUTING.md).
|
||
|
||
## 许可证
|
||
|
||
本项目采用 Apache License 2.0 许可 — 详情请参阅 [LICENSE.md](https://github.com/apify/crawlee/blob/master/LICENSE.md) 文件。
|