Puppeteer - API to control headless Chrome

Puppeteer - API to control headless Chrome

. 2 min read

I've used various options available for scraping in a headless mode including PhantomJS, Nightmare, ScraperJS, etc. Unfortunately, most of these options required xvfb which makes things slower, less reliable and utilizes a lot of memory.

Recently, Google launched Chrome that allows it to run in a true headless fashion. A few weeks later, Puppeteer which is:

a Node library which provides a high-level API to control headless Chrome over the DevTools Protocol. It can also be configured to use full (non-headless) Chrome.

Couple of things to note:

Puppeteer requires at least Node v6.4.0, but the examples below use async/await which is only supported in Node v7.6.0 or greater
When you install Puppeteer, it downloads a recent version of Chromium (~71Mb Mac, ~90Mb Linux, ~110Mb Win) that is guaranteed to work with the API.

I like this because it explicitly install all dependencies and managing all code is much easier!

I had run into few issues (like always!) while running headless chrome on my Ubuntu 16.04 LTS instance mentioned here.

Luckily, everything worked after installing the following mentioned here

My simple code is as follows:

npm init
npm -S puppeteer

n use latest test.js

I use n for node version management. The simple scrape test.js is:

const puppeteer = require('puppeteer');

(async() => {

const browser = await puppeteer.launch({headless: true});
const page = await browser.newPage();
await page.goto('https://google.com', {waitUntil: 'networkidle'});
// Type our query into the search bar
await page.type('puppeteer');

await page.click('input[type="submit"]');

// Wait for the results to show up
await page.waitForSelector('h3 a');

// Extract the results from the page
const links = await page.evaluate(() => {
  const anchors = Array.from(document.querySelectorAll('h3 a'));
  return anchors.map(anchor => anchor.textContent);
});
console.log(links.join('\n'));
browser.close();

})();

That's it! Awesome stuff!

puppeteer-demo


Tags

AWS Analytics Antifragile Apache Spark Apple Apps Arbitrage Automation Awesome BBMP Bangalore Black Swan Bonds Boonex Bootstrap Brokerage Charges CFPB CMS CSS Calypso Camelot Capital Markets Charts Chirp Choropleth Chrome Code Igniter Collections Containerization Converters Cycling DalalBeat Data Analytics Data Journalism Data Mining Data Munging Data Privacy Data Science Databases Debt Design Design Stock DesignStock Diet Django Docker Document Processing Dropbox Electron Electronics Entertainment Entrepreneurship Exercise FFMPEG Facebook Finance Fire TV Stick Fitness Forerunner From-The-News Fun Futures Gaana Garmin General Geocoding Getting Started Ghost Google APIs GraphicsMagick HHVM HTTPS Hackathon Hacks Hardware Health House Keeping Housing IBM BlueMix IBrokers IPinfo Ideas Image Conversion Image Processing Infographics Interactive Brokers Internet Investing IoT Legal Lets Encrypt Mac Machine Learning Mailbox app Mapbox Markdown Marketing Meteor Mockups MongoDB Mortgages Movies Music MyPad MySQL Nativefier Nature Netflix Newsletters Nginx Node.js Notes OCR OSX Open Source OpenCPU Options PDF PHP Pebble Peloton Personal Photography Picture Platforms Plex Portable Apps Productivity Property Tax PyroCMS Quantitative Finance R-Project Random Raspberry Pi Research Resources Reticulate Robot S3 SQLite STT Scraping Security Server Shell Shiny Side Project Sikuli Speech Recognition Spider Pro Spinning Spotify Startup Resources Startups Strategy Sublime Text System Monitoring Tax Terminal Tips To Do Tools Torrent Trading Trends Troubleshooting USElection2016 Ubuntu Unix Valuations Video Conversion Visualization Wagtail Wearables Weekend Fun WhatsApp Wordpress anaconda annie bash catalina cv2 daily coding dailycoding data.table debugging devtools downloader e-Signatures ggplot git go google maps hat-tip iOS iPad iPhone iTunes javascript jupyter logging macos numpy opencv optionality plotly python rstats videos youtube