项目作者: yujiosaka

项目描述 :
由Headless Chrome驱动的分布式爬虫
高级语言: JavaScript
项目地址: git://github.com/yujiosaka/headless-chrome-crawler.git
创建时间: 2017-12-02T07:12:12Z
项目社区:https://github.com/yujiosaka/headless-chrome-crawler

开源协议:MIT License

下载


Headless Chrome Crawler npm build Greenkeeper badge

API | Examples | Tips | Code of Conduct | Contributing | Changelog

Distributed crawler powered by Headless Chrome

Features

Crawlers based on simple requests to HTML files are generally fast. However, it sometimes ends up capturing empty bodies, especially when the websites are built on such modern frontend frameworks as AngularJS, React and Vue.js.

Powered by Headless Chrome, the crawler provides simple APIs to crawl these dynamic websites with the following features:

  • Distributed crawling
  • Configure concurrency, delay and retry
  • Support both depth-first search and breadth-first search algorithm
  • Pluggable cache storages such as Redis
  • Support CSV and JSON Lines for exporting results
  • Pause at the max request and resume at any time
  • Insert jQuery automatically for scraping
  • Save screenshots for the crawling evidence
  • Emulate devices and user agents
  • Priority queue for crawling efficiency
  • Obey robots.txt
  • Follow sitemap.xml
  • [Promise] support

Getting Started

Installation

  1. yarn add headless-chrome-crawler
  2. # or "npm i headless-chrome-crawler"

Note: headless-chrome-crawler contains Puppeteer. During installation, it automatically downloads a recent version of Chromium. To skip the download, see Environment variables.

Usage

  1. const HCCrawler = require('headless-chrome-crawler');
  2. (async () => {
  3. const crawler = await HCCrawler.launch({
  4. // Function to be evaluated in browsers
  5. evaluatePage: (() => ({
  6. title: $('title').text(),
  7. })),
  8. // Function to be called with evaluated results from browsers
  9. onSuccess: (result => {
  10. console.log(result);
  11. }),
  12. });
  13. // Queue a request
  14. await crawler.queue('https://example.com/');
  15. // Queue multiple requests
  16. await crawler.queue(['https://example.net/', 'https://example.org/']);
  17. // Queue a request with custom options
  18. await crawler.queue({
  19. url: 'https://example.com/',
  20. // Emulate a tablet device
  21. device: 'Nexus 7',
  22. // Enable screenshot by passing options
  23. screenshot: {
  24. path: './tmp/example-com.png'
  25. },
  26. });
  27. await crawler.onIdle(); // Resolved when no queue is left
  28. await crawler.close(); // Close the crawler
  29. })();

Examples

See here for the full examples list. The examples can be run from the root folder as follows:

  1. NODE_PATH=../ node examples/priority-queue.js

API reference

See here for the API reference.

Debugging tips

See here for the debugging tips.

FAQ

How is this different from other crawlers?

There are roughly two types of crawlers. One is static and the other is dynamic.

The static crawlers are based on simple requests to HTML files. They are generally fast, but fail scraping the contents when the HTML dynamically changes on browsers.

Dynamic crawlers based on PhantomJS and Selenium work magically on such dynamic applications. However, PhantomJS’s maintainer has stepped down and recommended to switch to Headless Chrome, which is fast and stable. Selenium is still a well-maintained cross browser platform which runs on Chrome, Safari, IE and so on. However, crawlers do not need such cross browsers support.

This crawler is dynamic and based on Headless Chrome.

How is this different from Puppeteer?

This crawler is built on top of Puppeteer.

Puppeteer provides low to mid level APIs to manupulate Headless Chrome, so you can build your own crawler with it. This way you have more controls on what features to implement in order to satisfy your needs.

However, most crawlers requires such common features as following links, obeying robots.txt and etc. This crawler is a general solution for most crawling purposes. If you want to quickly start crawling with Headless Chrome, this crawler is for you.