Fetching tweets with hashtag from Twitter using Python [closed]
Asked Answered
C

1

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

Consumer answered 4/1, 2013 at 11:49 Comment(1)
check out the following link. I've worked on it a bit. It might be helpful. plus.google.com/u/0/b/112388584507961481820/…Uraemia
P
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.

Proletariat answered 5/1, 2013 at 22:21 Comment(8)
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 authenticationConsumer
TwythonError Traceback (most recent call last) /home/vishal/<ipython-input-3-241f789b11cc> in <module>() ----> 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('api.twitter.com/1.1/search/tweets.json', params=kwargs) 368 369 def searchGen(self, search_query, **kwargs):Consumer
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'):Consumer
/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 contentConsumer
/usr/local/lib/python2.7/dist-packages/twython-2.5.5-py2.7.egg <br)/twython/twython.pyc in _request(self, url, method, params, files, api_call)<br> 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 errorConsumer
Hey Thanks Benjamin..................Cheers.I owe you beer for this buddyConsumer
The link to the docs in the second part is broken, please update the answer soonLivraison
A currently working docs link which seems to have some of the same info is github.com/ryanmcgrath/twython/blob/master/docs/usage/…Bowler

© 2022 - 2024 — McMap. All rights reserved.