How to extract Hashtags from Tweepy using tweepy.Cursor and api.search?

KubiK888 picture KubiK888 · Sep 17, 2014 · Viewed 14.1k times · Source

Tweepy has done a good job extracting all other information (except hashtags) I need by applying the tweepy.Cursor and api.search methods (as shown below). I know from the documentation that Hashtags are under this structure Status > entities > hashtags. And I tried to locate (below) the "hashtags" directory within the methods but to no avail:

print "tweet", dir(tweet)
print "////////////////"
print "tweet._api", dir(tweet._api)
print "////////////////"
print "tweet.text", dir(tweet.text)
print "////////////////"
print "tweet.entities", dir(tweet.entities)
print "////////////////"
print "tweet.author", dir(tweet.author)
print "////////////////"
print "tweet.user", dir(tweet.user)

My code is here:

import tweepy

ckey = ""
csecret = ""
atoken = ""
asecret = ""

OAUTH_KEYS = {'consumer_key':ckey, 'consumer_secret':csecret,
    'access_token_key':atoken, 'access_token_secret':asecret}
auth = tweepy.OAuthHandler(OAUTH_KEYS['consumer_key'], OAUTH_KEYS['consumer_secret'])
api = tweepy.API(auth)

for tweet in tweepy.Cursor(api.search, q=('"good book"'), since='2014-09-16', until='2014-09-17').items(5):

    print "Name:", tweet.author.name.encode('utf8')
    print "Screen-name:", tweet.author.screen_name.encode('utf8')
    print "Tweet created:", tweet.created_at
    print "Tweet:", tweet.text.encode('utf8')
    print "Retweeted:", tweet.retweeted
    print "Favourited:", tweet.favorited
    print "Location:", tweet.user.location.encode('utf8')
    print "Time-zone:", tweet.user.time_zone
    print "Geo:", tweet.geo
    print "//////////////////"

Answer

alecxe picture alecxe · Sep 17, 2014

Get the hashtags from the entities dictionary:

print tweet.entities.get('hashtags')