Web Scraping

scraping

Basic Tools

These tools do not use a real browser, so they’re perfect for scraping standards-based websites, but you may hit a wall trying to scrape sites that abuse scripting (angular or react client only sites, other single page applications …).

Less Basic Tools

These tools actually run in your own browser.

Ninja Tools

These tools run a scripted browser in the background, or even on a remote server or server farm. Reach for them when a site fights back: heavy JavaScript, or content that only appears after real browser interaction.

A notebook is a good place to drive any of these while you are still working out the selectors: see Jupyter.

Recipes

Using Chrome Developer tools to autoload an infinite scrollable list

Paste this in the console, let it run, then scrape the DOM once it stops. It scrolls to the bottom twice a second and gives up after three rounds that added nothing, which is how it detects the end of the list rather than spinning forever.

let last = 0, idle = 0;
const timer = setInterval(() => {
  window.scrollTo(0, document.body.scrollHeight);
  const h = document.body.scrollHeight;
  if (h === last) {
    if (++idle >= 3) { clearInterval(timer); console.log('done, height ' + h); }
  } else { last = h; idle = 0; }
}, 500);

Verified against a page that appends a batch of items on scroll, loaded in headless Chromium: 25 items before, 150 after, and the loop stopped on its own.

Polling the height is deliberate. The older version of this recipe hooked jQuery’s $(document).ajaxSuccess(...), which only works on a page that happens to load jQuery: in the DevTools console $ is a shortcut for document.querySelector, so the call fails everywhere else. Plenty of infinite scrolls do not use XHR anyway.

Ideas to explore

  • Distributing scraping work across machines with ZeroMQ or Dask.

Related

Keyboard shortcuts

/Open search
CtrlKOpen search (Cmd+K on macOS)
?Show this cheat sheet
Ctrl/Show this cheat sheet (Cmd+/ on macOS)
EscClose search or this cheat sheet

Turns off the single-key shortcuts (/, ?). Ctrl/Cmd+K and Ctrl/Cmd+/ stay.