Search code examples
ruby-on-railsrubytwitternokogirimechanize-ruby

Twitter API vs Web scraping?


To begin with I want to get all the followers of my twitter account. I did a little research and found that we can do web scraping with Ruby on rails using : Nokogiri or Mechanize gems. I also got a css selector to use for the web scraping. Now the HTML page source does not show all the followers of the account if I look it up.

Can I really use web scraping code to fetch all my twitter followers or Should I go for Twitter API?


Solution

  • In general terms, absolutely use APIs whenever possible.

    As the name implies, with "scraping" you are merely dealing with the "surface" of the application, in MVC terms its (HTML) views. Those views can change at any instant -- think how many times Twitter and other similar services undergo site redesigns. If you are scraping, then each site redesign, even a minor one, will very likely break your existing code, forcing you (without warning) to make frantic updates based on guesswork.

    Nokogiri and Mechanize are powerful tools, but they will never compare with the functionality, stability and consistency of an API, which accesses database content directly, bypassing the ever-changing "surface" altogether. In the case of Twitter, you have the added benefit of API wrappers such as the Twitter gem for accessing the API, which add a user-friendly layer to the API making it yet easier to integrate into your application.

    So to sum up: use the API, possibly via an API wrapper such as the Twitter gem.