skip to Main Content

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


  1. Using follow instead of track should work. With follow, you supply a list of user IDs:

    myStream.filter(follow=['14159148'])
    
    Login or Signup to reply.
  2. 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 for user_mentions and screen_name. Save only the one with your desired screen_name

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search