python - How to get media_url from tweets using the Tweepy API -
i using code:
import tweepy tweepy.api import api import urllib import os = 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: image in status.entities['media']: global #picname = status.user.screen_name picname = "pic%s.jpg" % += 1 link = image['media_url'] filename = os.path.join("c:/users/charbo/documents/python/",picname) urllib.urlretrieve(link,filename) #use 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'])
i trying access media_url under 'photo' in dictionary. getting following error: 'dict' object has no attribute 'media'. appreciate navigating json.
thanks in advance!
you should try 2 things :
- add entities request
>
tweepy.cursor(api.search, q="#hashtag", count=5, include_entities=true)
- check if media not nul :
>
if 'media' in tweet.entities: image in tweet.entities['media']: (do smthing image['media_url'])
hope help
Comments
Post a Comment