Skip to content

Instantly share code, notes, and snippets.

@MiguelSavignano
Created March 1, 2017 17:41
Show Gist options
  • Save MiguelSavignano/5ee2ce98cd0669837ae0070ea672069e to your computer and use it in GitHub Desktop.
Save MiguelSavignano/5ee2ce98cd0669837ae0070ea672069e to your computer and use it in GitHub Desktop.
instagram scrape ruby
instagram_username = "foofighters"
def instagram_scraper(instagram_username)
require "mechanize"
mechanize = Mechanize.new
page = mechanize.get("https://www.instagram.com/#{instagram_username}")
scripts = page.search("script").map(&:to_s)
script = scripts.select{|script| /window._sharedData =/ =~ script}.first
_sharedData = JSON.parse(script.split("window._sharedData = ").second.split(";</script>").first)
feed = _sharedData.dig('entry_data','ProfilePage', 0, 'user', 'media', 'nodes') || []
end
# example
# [
# {
# "__typename":"GraphImage",
# "id":"1459689546726893592",
# "comments_disabled":false,
# "dimensions":{
# "height":742,
# "width":750
# },
# "owner":{
# "id":"4574754"
# },
# "thumbnail_src":"https://instagram.fmad3-2.fna.fbcdn.net/t51.2885-15/s640x640/sh0.08/e35/c4.0.742.742/17076123_494530134267787_3148910248643788800_n.jpg",
# "is_video":false,
# "code":"BRB2-izAegY",
# "date":1488228575,
# "display_src":"https://instagram.fmad3-2.fna.fbcdn.net/t51.2885-15/e35/17076123_494530134267787_3148910248643788800_n.jpg",
# "caption":"Get ready for many more surprises... Sign up for the newsletter at FooFighters.com to be the first to know!",
# "comments":{
# "count":185
# },
# "likes":{
# "count":21451
# }
# }
# ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment