I’m tracking all mentions of @UN with Tweepy using Twitter stream v1 API. However, I’m also getting all mentions of usernames containing @UN such as @UN_Women. I could filter them out in a post-processing step but this seems very inefficient.
Is there any way to avoid this?
This is my code:
class MyStreamListener(tweepy.StreamListener):
def on_status(self, status):
print(status.text)
myStreamListener = MyStreamListener()
myStream = tweepy.Stream(auth = api.auth, listener=myStreamListener())
myStream.filter(track=['@UN'])
2
Answers
Using
follow
instead oftrack
should work. Withfollow
, you supply a list of user IDs:I don’t know if tweepy provides any further functionalities to avoid this. But what you can do here is, filter out the results while saving it to your database or csv.
Check the json response and look for
entities
object and in that check foruser_mentions
andscreen_name
. Save only the one with your desiredscreen_name