headless-chrome-crawler 是一个自带 JavaScript 执行环境的爬虫插件。它支持分布式,是一款分布式爬虫。它能解决 AngularJS、vue.js 等这些现代化的前端框架编写的网站的爬虫问题。本文将详细的介绍它的相关教程。
前面我写过《Webmagic(爬虫)抓取新浪博客案例》Webmagic 是一个基于 java 实现的爬虫框架,关于它的相关教程都可以在我的博客中找到。
特征
- 分布式抓取
- 配置并发性,延迟和重试
- 支持深度优先搜索和广度优先搜索算法
- 可插拔的缓存存储,如Redis
- 支持导出结果的CSV和JSON行
- 在最大请求时暂停并在任何时候恢复
- 自动插入jQuery进行刮取
- 保存抓取证据的截图
- 仿真设备和用户代理
- 优先队列提高抓取效率
- 服从robots.txt
- 跟随sitemap.xml
安装
headless-chrome-crawler 有两种安装方式,如下:
yarn add headless-chrome-crawler # or "npm i headless-chrome-crawler"
爬行器包含Puppeteer。在安装过程中,它会自动下载最新版本的Chromium。
用法
它的用法很简单,如下:
const HCCrawler = require('headless-chrome-crawler');
HCCrawler.launch({
// Function to be evaluated in browsers
evaluatePage: (() => ({
title: $('title').text(),
})),
// Function to be called with evaluated results from browsers
onSuccess: (result => {
console.log(result);
}),
})
.then(crawler => {
// Queue a request
crawler.queue('https://example.com/');
// Queue multiple requests
crawler.queue(['https://example.net/', 'https://example.org/']);
// Queue a request with custom options
crawler.queue({
url: 'https://example.com/',
// Emulate a tablet device
device: 'Nexus 7',
// Enable screenshot by passing options
screenshot: {
path: './tmp/example-com.png'
},
});
crawler.onIdle() // Resolved when no queue is left
.then(() => crawler.close()); // Close the crawler
});
另外 github 上还提供了很多案例和 api 文档的使用。更多用法请移步到 github 上自行学习。

: » headless-chrome-crawler 教程
原创文章,作者:carmelaweatherly,如若转载,请注明出处:https://blog.ytso.com/tech/aiops/251364.html