问题
I started exploring tweepy a couple days ago and was able to stream filtered (with keywords) tweets at real time. Now I want to stream not only filtered tweets but also tweets from several specific twitter users. Is this possible by using tweepy? It seems stream.userstream() only fetches real-time tweets from my twitter account NOT from other specific users, right? I have tried it with another twitter account that I created for testing but it does not fetch any new tweets that I tweeted, at all.
But if it works, can I download tweets using stream.userstream() and stream.filter() at the same time? If not then how can I get both filtered tweets and users' real time tweets?
Btw I used the example code from @alexhanna.
api = tweepy.API(auth)
def main( mode = 1 ):
follow = []
track = ['Houston Rockets','Lakers','Chicago Bulls']
listen = SListener(api, 'test')
stream = tweepy.Stream(auth, listen)
try:
stream.userstream('NBA','ESPN')
stream.filter(track = track, follow = follow)
except:
print "error!"
stream.disconnect()
Would really appreciate your help! Thanks.
回答1:
Try using the .filter(follow="")
without .userstream()
beforehand. Userstream
is only the tweets from the account associated with your application. Here is a (very well-annotated) example.
If you want to get the user's tweets and filtered tweets at the same time you need to make two separate Stream()
objects.
Edit: the page I linked to is now dead. The Internet Archive link should remain active indefinitely, but all the relevant information to solving the user's question is already contained within this answer. I have not copied and pasted the example from the linked page as I am not the author of it, and because it is only an example that illustrates the proper use of a Stream
listener.
回答2:
If you want to stream specific user time-line:
1) Find his/her ID with this website or google for How to find twitter ID.
2) Use following code:
from tweepy import OAuthHandler
from tweepy import Stream
from tweepy import StreamListener
listener = StreamListener()
auth = OAuthHandler(config.API_KEY, config.API_SECRET)
auth.set_access_token(config.ACCESS_TOKEN, config.ACCESS_TOKEN_SECRET)
stream = Stream(auth, listener)
stream.filter(follow=['3511430425']) # user ID for random account
来源:https://stackoverflow.com/questions/22675561/using-tweepy-to-stream-users-timeline-and-filtered-tweets