5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

#Twitter #API でタイムラインから、メディアの画像URLつきの #Json を得る #python スクリプトの例

Last updated at Posted at 2019-02-11

#Twitter #API でタイムラインから、メディアの画像URLつきの #Json を得る #python スクリプトの例

ツイッタランドの森の奥深く‥

画像URLは奥深くに隠されている。
ツイートのURLを与えると、Twitterが勝手に画像を展開する仕様。
画像直接のURLはなか見当たらない。
HTMLソース見てもmedia id さえ見当たらない。なんかJsとかで展開させてるはず。

ありがとうStackoverflow

Are you adding tweet_mode=extended on your API call to get the new extended Tweet format?

No media_url in reslt of statuses/user_timeline for some statuses - REST API - Twitter Developers

Script

これで解決!!!!!!

  'tweet_mode' : 'extended'
import json, config #標準のjsonモジュールとconfig.pyの読み込み
from requests_oauthlib import OAuth1Session #OAuthのライブラリの読み込み

CK = config.CONSUMER_KEY
CS = config.CONSUMER_SECRET
AT = config.ACCESS_TOKEN
ATS = config.ACCESS_TOKEN_SECRET
twitter = OAuth1Session(CK, CS, AT, ATS) #認証処理

url = "https://api.twitter.com/1.1/statuses/user_timeline.json" #タイムライン取得エンドポイント

params ={
  'count' : 5,
  'exclude_replies' : True,
  'tweet_mode' : 'extended'

}
res = twitter.get(url, params = params)

if res.status_code == 200: #正常通信出来た場合
    timelines = json.loads(res.text) #レスポンスからタイムラインリストを取得
    for line in timelines: #タイムラインリストをループ処理
        print(line)
else: #正常通信出来なかった場合
    print("Failed: %d" % res.status_code)

とれたよ!

image

実物

image

Ref

PythonでTwitter API を利用していろいろ遊んでみる - Qiita

Original by Github issue

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

5
6
0

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
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?