Skip to content

Latest commit

 

History

History
81 lines (57 loc) · 2.56 KB

TIPS.md

File metadata and controls

81 lines (57 loc) · 2.56 KB

Tips

Distributed crawling

In order to crawl under distributed mode, use Redis for the shared cache storage. You can run the same script on multiple machines, so that Redis is used to share and distribute task queues.

const HCCrawler = require('headless-chrome-crawler');
const RedisCache = require('headless-chrome-crawler/cache/redis');

const TOP_PAGES = [
  // ...
];

const cache = new RedisCache({
  // ...
});

(async () => {
  const crawler = await HCCrawler.launch({
    maxDepth: 3,
    cache,
  });
  await crawler.queue(TOP_PAGES);
})();

Launch options

HCCrawler.launch()'s options are passed to puppeteer.launch(). It may be useful to set the headless and slowMo options so that you can see what is going on.

HCCrawler.launch({ headless: false, slowMo: 10 });

Also, the args option is passed to the browser instance. List of Chromium flags can be found here. Passing --disable-web-security flag is useful for crawling. If the flag is set, links within iframes are collected as those of parent frames. If it's not, the source attributes of the iframes are collected as links.

HCCrawler.launch({ args: ['--disable-web-security'] });

Running tests

All tests but RedisCache's are run by the following command:

yarn test

When you modify RedisCache's code, make sure that Redis is installed, start the server and run all tests with the following command:

yarn test-all

Enable debug logging

All requests and browser's logs are logged via the debug module under the hccrawler namespace.

env DEBUG="hccrawler:*" node script.js
env DEBUG="hccrawler:request" node script.js
env DEBUG="hccrawler:browser" node script.js

Crawl in Docker

Build the container with this Dockerfile:

docker build -t headless-chrome-crawler-linux .

Run the container by passing node -e "<yourscript.js content as a string>" as the command:

docker run -i --rm --cap-add=SYS_ADMIN \
  --name headless-chrome-crawler headless-chrome-crawler-linux \
  node -e "`cat yourscript.js`"