Skip to content Skip to sidebar Skip to footer

Twitter Api: How To Exclude Retweets When Searching Tweets Using Twython

I'm trying to exclude retweets and replies in my Twython search. Here is my code: from twython import Twython, TwythonError app_key = 'xxxx' app_secret = 'xxxx' oauth_token = 'xxx

Solution 1:

The correct syntax is -filter:retweets.

If you would like to search on terms "search phrase" or "another search phrase" and exclude retweets, then the query should be:

query = "search_phrase OR another_search_phrase -filter:retweets"

To exclude replies as well, add -filter:replies like this:

query = "search_phrase OR another_search_phrase -filter:retweets AND -filter:replies"

This should be working, you can verify it by checking the status fields in_reply_to_status_id and retweeted_status:

  • Status is not a reply if in_reply_to_status_id is empty
  • Status is not a retweet if it doesn't have the field retweeted_status

With Twython:

import twython

twitter = twython.Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET) 

query = 'wine OR beer -filter:retweets AND -filter:replies' 
response = twitter.search(q=query, count=100)
statuses = response['statuses']
forstatusin statuses:
    printstatus['in_reply_to_status_id'], status.has_key('retweeted_status')

# Output should be (None, False) to any status

Post a Comment for "Twitter Api: How To Exclude Retweets When Searching Tweets Using Twython"