【发布时间】:2016-08-11 04:29:03
【问题描述】:
我正在使用此代码:
import tweepy
from tweepy.api import API
import urllib
import os
i = 1
consumer_key="xx"
consumer_secret="xx"
access_token="xx"
access_token_secret="xx"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.secure = True
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
class MyStreamListener(tweepy.StreamListener):
def __init__(self, api=None):
self.api = api or API()
self.n = 0
self.m = 10
def on_status(self, status):
if 'media' in status.entities:
for image in status.entities['media']:
global i
#picName = status.user.screen_name
picName = "pic%s.jpg" % i
i += 1
link = image['media_url']
filename = os.path.join("C:/Users/Charbo/Documents/Python/",picName)
urllib.urlretrieve(link,filename)
#use to test
print(status.user.screen_name)
else:
print("no media_url")
self.n = self.n+1
if self.n < self.m:
return True
else:
print ('tweets = '+str(self.n))
return False
def on_error(self, status):
print (status)
myStreamListener = MyStreamListener()
myStream = tweepy.Stream(auth, MyStreamListener(),timeout=30)
myStream.filter(track=['#feelthebern'])
我正在尝试访问我字典中“照片”下的 media_url。但我收到以下错误:'dict' 对象没有属性'media'。我将不胜感激帮助导航 JSON。
提前致谢!
【问题讨论】:
-
您是否尝试查看所有 dir(status) 提供的内容?我无法重现该错误,因为您没有给出最小和完整的步骤来重现它,但是使用了 twitters api,我认为 json 在类似 status._json 的东西中可用?
-
@MohitC 我添加了所有代码。