instagram api

$ install instaloader

import instaloader

# インスタンスの作成
L = instaloader.Instaloader()

# 取得したいユーザーのIDを指定
target_profile = "instagram"  # 例としてInstagram公式アカウント

try:
    # プロフィール情報の読み込み
    profile = instaloader.Profile.from_username(L.context, target_profile)

    print(f"ユーザー名: {profile.username}")
    print(f"フォロワー数: {profile.followers}")
    print(f"自己紹介: {profile.biography}")

    # 最新の投稿を3件表示
    print("\n--- 最新の投稿 ---")
    for post in profile.get_posts():
        print(f"投稿日: {post.date}")
        print(f"キャプション: {post.caption[:30]}...") # 最初の30文字だけ
        print(f"URL: {post.url}")
        
        # 3件取得したらストップ
        if post.owner_username == target_profile:
            break 

except Exception as e:
    print(f"エラーが発生しました: {e}")

$ python3 app.py
ユーザー名: instagram
フォロワー数: 698490388
自己紹介: Discover what’s new on Instagram 🔎✨

— 最新の投稿 —
投稿日: 2025-12-26 21:05:21
キャプション: the definition of “never let t…
URL: https://scontent-nrt1-2.cdninstagram.com/v/t51.2885-15/606469868_18689042716001321_9120174716535860606_n.jpg?stp=dst-jpg_e15_fr_p1080x1080_tt6&_nc_ht=scontent-nrt1-2.cdninstagram.com&_nc_cat=1&_nc_oc=Q6cZ2QFZnredhbdVl_SvagHdS1_L6FOS6xwQKfiSolX7tuMyxIYTOVN7Um-LzzQ_jBneDHI&_nc_ohc=VXPBA7g5rlQQ7kNvwFrzZcc&_nc_gid=s6qDPQ_v2Qy2aL1kVrz0Wg&edm=AOQ1c0wBAAAA&ccb=7-5&oh=00_AfmMpzoxHZbJe-l-Inrc-cUqHsm3Nfz13kckIgLq0ttU4A&oe=69584AA3&_nc_sid=8b3546

instaloader は、ブラウザでインスタを見る時と同じような仕組みで情報を読み取っているため、**「公開アカウント」かつ「数件の取得」**であれば、面倒なAPIキーの手続きなしでサクッと動く

「仕事で使いたい」「公式にアプリをリリースしたい」となった場合は、Meta社が提供する**「Instagram Graph API」**を使う必要がある

import instaloader

L = instaloader.Instaloader()

# 検索したいハッシュタグ(#は不要)
hashtag_name = "猫"

try:
    # ハッシュタグオブジェクトの作成
    hashtag = instaloader.Hashtag.from_name(L.context, hashtag_name)

    print(f"#{hashtag_name} の投稿を取得中...")

    # 最新の投稿を5件ループで回す
    for count, post in enumerate(hashtag.get_posts(), 1):
        print(f"\n[{count}件目]")
        print(f"投稿者: {post.owner_username}")
        print(f"いいね数: {post.likes}")
        print(f"キャプション: {post.caption[:50]}...") # 50文字まで
        print(f"URL: {post.url}")

        # 負荷軽減のため5件でストップ
        if count >= 5:
            break

except Exception as e:
    print(f"エラーが発生しました: {e}")

$ python3 cat.py
JSON Query to api/v1/tags/web_info/: 403 Forbidden – “fail” status, message “login_required” when accessing https://i.instagram.com/api/v1/tags/web_info/?__a=1&__d=dis&tag_name=%E7%8C%AB [retrying; skip with ^C]
JSON Query to api/v1/tags/web_info/: 403 Forbidden – “fail” status, message “login_required” when accessing https://i.instagram.com/api/v1/tags/web_info/?__a=1&__d=dis&tag_name=%E7%8C%AB [retrying; skip with ^C]
エラーが発生しました: JSON Query to api/v1/tags/web_info/: 403 Forbidden – “fail” status, message “login_required” when accessing https://i.instagram.com/api/v1/tags/web_info/?__a=1&__d=dis&tag_name=%E7%8C%AB

タグ検索の場合は、ログインが必要となる。
L.login(“自分のユーザー名”, “パスワード”)

本格的にやりたい場合はInstagram Graph API

その他

import instaloader

L = instaloader.Instaloader()

# --- 修正ポイント:パスワードを使わずブラウザのクッキーを使う ---
# Chromeがインストールされている場合、以下の1行でセッションを読み込めます
# 注意: 初回実行時にOSからブラウザデータへのアクセス許可を求められることがあります
USER = ""
try:
    L.interactive_login(USER) # または L.load_session_from_instaloader(USER)
    # 最も簡単な方法:
    # ブラウザのクッキーをインポートするヘルパー関数(instaloader公式推奨)
    # ※ pip install browser-cookie3 が必要な場合があります
    # 以下の1行でブラウザのログイン状態を引き継ぎます
    L.load_session_from_instaloader(USER) 
except:
    # ブラウザからセッションを取得するスクリプトを別途実行するか
    # 以下のコマンドをターミナルで実行してセッションファイルを作ってください
    # instaloader -l YOUR_USER_NAME
    print("セッションの読み込みに失敗しました。")

# 実行
hashtag_name = "猫"
hashtag = instaloader.Hashtag.from_name(L.context, hashtag_name)

for count, post in enumerate(hashtag.get_posts(), 1):
    print(f"[{count}] {post.owner_username}: {post.caption[:20]}...")
    if count >= 3: break