tweepy从json响应获取媒体网址

时间:2018-06-08 08:35:15

标签: python python-3.x tweepy

我想从其JSON响应中获取twitter图像URL。我使用了python脚本。下面是代码。

import re
import tweepy
import sys
consumer_key = 'xxxx'
consumer_secret = 'xxx'
access_token ='xxxx'
access_token_secret = 'xxxx'

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
media_files = set()
for status in tweepy.Cursor(api.home_timeline,screen_name='@yyyyy').items(250):
    media = status.entities.get('media', [])
    if(len(media) > 0):
            media_files.add(media[0]['media_url'])
            url=status._json['media_url']
            print(url)

我需要从JSON响应中处理图像URL并打印它。它显示如下错误消息。

url=status._json['media_url'] KeyError: 'media_url'

1 个答案:

答案 0 :(得分:1)

for status in tweepy.Cursor(api.home_timeline,screen_name='@yyyyy').items(250):
    if 'media' in status.entities:
        for image in status.entities['media']:
            print(image['media_url'])