I Don’t Need No Stinking API – Web Scraping in 2016 and Beyond

Social media APIs and their rate limits have not been nice to me recently, especially Instagram. Who needs it anyway?

Sites are increasingly getting smarter against scraping / data mining attempts. AngelList even detects PhantomJS (have not seen other sites do this). But if you are automating your exact actions that happen via a browser, can this be blocked?

I’m going to share everything that I’ve learnt to date from my recent love affair with Selenium automation/scraping/crawling. The purpose of this post is to illustrate some of the techniques I’ve created which I haven’t seen anywhere – as a broader idea to be shared around, rather than a how-to.

First off, in terms of concurrency or the amount of horsepower you get for your hard earned $$$ – Selenium sucks. It’s simply not built for what you would consider ‘scraping’. But with sites being built with more and more smarts these days, the only truely reliable way to mine data off the internets is to use browser automation.

My stack looks like, pretty much all JavaScript. There goes a few readers 😑😆 – WebdriverIO, Node.js and a bunch of NPM packages including the likes of antigate (thanks to Troy Hunt –  Breaking CAPTCHA with automated humans) but I’m sure most of my techniques can be applied to any flavour of the Selenium 2 driver. It just happens that I find coding JavaScript optimal for browser automation.

Faking Human Delays

It’s definitely good practice to add these human-like, random pauses in some places just to be extra safe:

Parsing Data jQuery Style with Cheerio

Below is a snippet from a function that gets videos from a Facebook page which I used for Skater.Life and Hustle.Vision

 

I also use this similar method and some regex to parse RSS feeds that can’t be read by command line, cURL-like scripts.

It actually works pretty well – I’ve tested with multiple sources.

Injecting JavaScript

If you get to my level, injecting JavaScript for the client-side becomes commonplace.

By the way, this is a totally non-practical example (in case you haven’t noticed). Check out the following headings.

Beating CAPTCHA

GIFLY.co had not updated for over 48 hours and I wondered why. My script which gets animated gifs from various Facebook pages was being hit with the capture screen 😮

Cracking Facebook’s captcha was actually pretty easy. It took me exactly 15 minutes to accomplish this. I’m sure there are ways to do this internally but with Antigate providing an NPM package and with costs so low, it was a no-brainer for me.

 

So injecting JavaScript has become super-handy here. I’m converting an image to a canvas, then running .toDataURL() to get a Base64 encoded PNG image to send to the Antigate endpoint. The function was stolen from a site where I steal a lot of things from, shouts to David Walsh. This solves the Facebook captcha, enters the value then clicks submit.

Catching AJAX Errors

Why would you want to catch client-side AJAX errors? Because reasons. For example, I automated unfollowing everyone on Instagram and I found that even through their website (not via the API) there is some kind of a rate limit.

Because a follow/unfollow invokes an AJAX call, and being rate limited would mean an AJAX error, I inject an AJAX error capturing function then save it to a global variable.

I retrieve this value after each unfollow and terminate the script if I get 3 errors.

Intercepting AJAX Data

While scraping/crawling/spidering Instagram, I ran into a problem. A tag page did not give me the post date in the DOM. I really needed this data for IQta.gs and I couldn’t afford visiting every post as I’m parsing about 200 photos every time.

What I did find though, is that there is a date variable stored in the post object that the browser receives. Heck, I ended up not even using this variable but this is what I came up with:

It’s getting a little late in Melbourne.

Other Smarts

So I run all of this in a Docker container running on AWS. I’ve pretty much made my Instagram crawlers fault-tolerant with some bash scripting (goes to check if it is running now)

Yes, it seems that all 6 IQta.gs crawlers are running fine 🙂 I’ve run into some issues with Docker where an image becomes unusable, I have no idea why – I did not spend time to look into the root cause, but basically my bash script will detect non-activity and completely remove and start the Selenium grid again from a fresh image.

Random Closing Thoughts

I had this heading written down before I started writing this post and I have forgotten the random thoughts I had back then. Oh well, maybe it will come back tomorrow.

Ah, a special mention goes out to Hartley Brody and his post, which was a very popular article on Hacker News in 2012/2013 – it inspired me to write this.

Those of you wondering what the hell browser is:

And argv comes from yargs

Thanks to my long time friend In-Ho @ Google for proofreading! 🙂

Follow me

Francis Kim

Since early-mid 2000s, my career's been mostly focused on eCommerce (Magento) and the sites I've worked on so far generate over AUD 150M+ revenue each year. I believe JavaScript and Automation is the future!
Follow me

6 Comments

Add yours →

  1. Francis, very impressive. I like the Catching Ajax errors part.
    Thanks a ton for all the useful snippets!

  2. Just curious, how would you make money with scraping??

    I’m a JS developer for profession, and have to setup Selenium + mocha / nightwatch frequently.

    So wonder if there is some way to make an extra bug 🙂

    • Not sure to be honest lol. Most scraping jobs seem to be kind of low quality jobs wanting x leads for y dollars – that’s certainly not what I am after. But it has served me well for personal use. I think the money will be in creating a bot that allows smart automation rather than just doing the numbers.

    • Lead-gen jobs are all over the place, but they don’t pay very well unless you can scrape multiple sources and cross-reference to improve lead quality (which usually takes some data-science experience).

  3. Your logo and specially it’s colors resembles speedof.me. Haha

Leave a Reply