LoginSignup
20
23

More than 5 years have passed since last update.

InstagramをRubyでスクレイピングする

Posted at

Instagramの規約が2016/6/1から厳格になり、APIの使用に制限がかかりました。
趣味で運用していたサービスが動かなくなったので、Instagramの本サイトからスクレイピングできないかやってみました。そうしたところ、できたのでコードを共有します。

コード

open-uriとJSONを使っています。Instagramのuser_idを引数に入れて使います。ユーザー情報と最新の画像が取得できます。

require 'open-uri'
require 'JSON'

def scrape_instagram(user_id)
  begin
    instagram_source = open("https://www.instagram.com/#{user_id}").read
    content = JSON.parse(instagram_source.split("window._sharedData = ")[1].split(";</script>")[0])
    return content['entry_data']['ProfilePage'][0]['user']
  rescue Exception => e
    return nil
  end
end

Usage

使い方です。

scrape_instagram("nyanchan22")

Githubでコードを公開しているので、もしよければスターして下さい。
https://github.com/naoya0731/scrape_instagram_ruby

20
23
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
23