• Stars
    star
    5,393
  • Rank 7,640 (Top 0.2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Distributed crawler powered by Headless Chrome

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

yarn add headless-chrome-crawler
# 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

const HCCrawler = require('headless-chrome-crawler');

(async () => {
  const crawler = await 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);
    }),
  });
  // Queue a request
  await crawler.queue('https://example.com/');
  // Queue multiple requests
  await crawler.queue(['https://example.net/', 'https://example.org/']);
  // Queue a request with custom options
  await crawler.queue({
    url: 'https://example.com/',
    // Emulate a tablet device
    device: 'Nexus 7',
    // Enable screenshot by passing options
    screenshot: {
      path: './tmp/example-com.png'
    },
  });
  await crawler.onIdle(); // Resolved when no queue is left
  await crawler.close(); // Close the crawler
})();

Examples

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

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.

More Repositories

1

js-mind

Deep Learning Library Written in ES2015.
JavaScript
184
star
2

ChatIQ

ChatIQ is a versatile Slack bot using GPT & Weaviate-powered long-term memory to accomplish various tasks.
Python
17
star
3

hubot-github-comment-notifier

A GitHub pull request and issue comment notifier for hubot
CoffeeScript
11
star
4

puppet-king-of-time

Have KING OF TIME (www.kingtime.jp) like a puppet on a string
TypeScript
8
star
5

neural-networks-and-deep-learning-js

A JavaScript implementation for "Neural Networks and Deep Learning" by Michael Nielsen
CoffeeScript
6
star
6

native_alert

A Flutter plugin to trigger native alert and confirm dialogs.
Dart
5
star
7

request-notification-permission-demo

CSS
3
star
8

circleci-dynamic-config-example

An example app in Rust for CircleCI's Dynamic Config demo
Rust
3
star
9

website-macro-recorder

Record macros on website
Kotlin
2
star
10

cross-site-tab-change-detection

Demo for cross site tab change detection
TypeScript
2
star
11

socke.io-ie8-loading-example

A sample app to illustrate ie8 loading triggered by socket.io
JavaScript
2
star
12

brain-js-live-demo

Live demo for https://speakerdeck.com/yujiosaka/machine-learning-with-javascript
HTML
2
star
13

WDB109_Puppeteer

WEB+DB PRESS Vol.109 特集「速習Puppeteer──ヘッドレスChromeでテスト&スクレイピング」サンプルコード
JavaScript
2
star
14

socket.io-cache-example

A sample app to illustrate page cache is cleared by socket.io
JavaScript
2
star
15

slack-king-of-time

A bot to automates clocking in and out using KING OF TIME (www.kingtime.jp).
TypeScript
2
star
16

causal-inference-demo-with-gurobi-machine-learning

Causal inference demo with Gurobi Machine Learning
Jupyter Notebook
1
star
17

browserify-jquery-test

A proof of concept that browserify encapsulates jQuery object
JavaScript
1
star
18

circleci-parallelism-option-test

Testing the behavior of CircleCI concurrency option
TypeScript
1
star
19

cross-tab-communication

Demo for cross tab communication
TypeScript
1
star
20

vrp-demo-with-oxmnx-and-ortools

Vehicle Routing Problem (VRP) demo with OSMnx and OR-Tools
Jupyter Notebook
1
star