Fetching tweets with hashtag from Twitter using Python

Uselesssss picture Uselesssss · Jan 4, 2013 · Viewed 23.5k times · Source

How do we find or fetch tweets on the basis of hash tag. i.e. I want to find tweets regarding on a certain subject? Is it possible in Python using Twython?

Thanks

Answer

Benjamin White picture Benjamin White · Jan 5, 2013

EDIT My original solution using Twython's hooks for the Search API appears to be no longer valid because Twitter now wants users authenticated for using Search. To do an authenticated search via Twython, just supply your Twitter authentication credentials when you initialize the Twython object. Below, I'm pasting an example of how you can do this, but you'll want to consult the Twitter API documentation for GET/search/tweets to understand the different optional parameters you can assign in your searches (for instance, to page through results, set a date range, etc.)

from twython import Twython

TWITTER_APP_KEY = 'xxxxxx'  #supply the appropriate value
TWITTER_APP_KEY_SECRET = 'xxxxxx' 
TWITTER_ACCESS_TOKEN = 'xxxxxxx'
TWITTER_ACCESS_TOKEN_SECRET = 'xxxxxx'

t = Twython(app_key=TWITTER_APP_KEY, 
            app_secret=TWITTER_APP_KEY_SECRET, 
            oauth_token=TWITTER_ACCESS_TOKEN, 
            oauth_token_secret=TWITTER_ACCESS_TOKEN_SECRET)

search = t.search(q='#omg',   #**supply whatever query you want here**
                  count=100)

tweets = search['statuses']

for tweet in tweets:
  print tweet['id_str'], '\n', tweet['text'], '\n\n\n'

Original Answer

As indicated here in the Twython documentation, you can use Twython to access the Twitter Search API:

from twython import Twython
twitter = Twython()
search_results = twitter.search(q="#somehashtag", rpp="50")

for tweet in search_results["results"]:
    print "Tweet from @%s Date: %s" % (tweet['from_user'].encode('utf-8'),tweet['created_at'])
    print tweet['text'].encode('utf-8'),"\n"

etc... Note that for any given search, you're probably going to max out at around 2000 tweets at most, going back up to around a week or two. You can read more about the Twitter Search API here.