Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetching tweets with hashtag from Twitter using Python [closed]

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

like image 750
Uselesssss Avatar asked Jan 04 '13 11:01

Uselesssss


People also ask

How do I extract tweets from a hashtag?

Step-by-step Approach:Import required modules. Create an explicit function to display tweet data. Create another function to scrape data regarding a given Hashtag using tweepy module. In the Driver Code assign Twitter Developer account credentials along with the Hashtag, initial date and number of tweets.

Can you scrape twitter with Python?

Tweepy is a Python library for integrating with the Twitter API. Because Tweepy is connected with the Twitter API, you can perform complex queries in addition to scraping tweets. It enables you to take advantage of all of the Twitter API's capabilities.


1 Answers

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.

like image 129
Benjamin White Avatar answered Sep 23 '22 23:09

Benjamin White