6

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

Joe C
  • 3,506
  • 2
  • 21
  • 32
Uselesssss
  • 2,127
  • 6
  • 28
  • 37
  • 1
    check out the following link. I've worked on it a bit. It might be helpful. https://plus.google.com/u/0/b/112388584507961481820/112388584507961481820/posts – Prabhat Kumar Aug 14 '13 at 15:30

1 Answers1

20

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.

sanooj
  • 493
  • 5
  • 12
Benjamin White
  • 779
  • 6
  • 25
  • hey Thanks again ,but i am getting an error while i do this The following is an error which i am getting. Is it that it requires authentication – Uselesssss Jan 07 '13 at 13:50
  • TwythonError Traceback (most recent call last) /home/vishal/ in () ----> 1 search_results = twitter.search(q="#india", rpp="50") /usr/local/lib/python2.7/dist-packages/twython-2.5.5-py2.7.egg/twython/twython.pyc in search(self, **kwargs) 365 """ 366 --> 367 return self.get('https://api.twitter.com/1.1/search/tweets.json', params=kwargs) 368 369 def searchGen(self, search_query, **kwargs): – Uselesssss Jan 07 '13 at 13:51
  • usr/local/lib/python2.7/dist-packages/twython-2.5.5-py2.7.egg/twython/twython.pyc in get(self, endpoint, params, version) 236 237 def get(self, endpoint, params=None, version='1.1'): --> 238 return self.request(endpoint, params=params, version=version) 239 240 def post(self, endpoint, params=None, files=None, version='1.1'): – Uselesssss Jan 07 '13 at 13:52
  • /usr/local/lib/python2.7/dist-packages/twython-2.5.5-py2.7.egg/twython/twython.pyc in request(self, endpoint, method, params, files, version) 231 url = '%s/%s.json' % (self.api_url % version, endpoint) 232 --> 233 content = self._request(url, method=method, params=params, files=files, api_call=url) 234 235 return content – Uselesssss Jan 07 '13 at 13:53
  • /usr/local/lib/python2.7/dist-packages/twython-2.5.5-py2.7.egg
    208 raise exceptionType(error_msg, 209 error_code=response.status_code, --> 210 retry_after=response.headers.get('retry-after')) 211 212 # if we have a json error here, then it's not an official TwitterAPI error
    – Uselesssss Jan 07 '13 at 13:54
  • 1
    Hey Thanks Benjamin..................Cheers.I owe you beer for this buddy – Uselesssss Jan 08 '13 at 04:01
  • The link to the docs in the second part is broken, please update the answer soon – Bhargav Rao Apr 16 '16 at 04:19
  • A currently working docs link which seems to have some of the same info is https://github.com/ryanmcgrath/twython/blob/master/docs/usage/basic_usage.rst – alttag Oct 16 '17 at 19:35