简体   繁体   中英

Twitter v1 stream API returns wrong mentions

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'])

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

myStream.filter(follow=['14159148'])

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM