如何使用tweepy.Cursor和api.search从Tweepy中提取Hashtags?

时间:2014-09-17 16:02:47

标签: python-2.7 twitter hashtag tweepy

通过应用tweepy.Cursor和api.search方法(如下所示),Tweepy已经很好地提取了我需要的所有其他信息(除了主题标签)。我从文档中知道Hashtags属于这种结构状态>实体>井号标签。我试图找到(下面)" hashtags"方法中的目录但无济于事:

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)

我的代码在这里:

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 "//////////////////"

2 个答案:

答案 0 :(得分:3)

hashtags获取entities dictionary

print tweet.entities.get('hashtags')

答案 1 :(得分:1)

没有代表要评论,而是要回答Fabian Bosler的问题-由于实体是字典,请尝试

tweet.entities['hashtags']

对我有用。