使用Python从Twitter获取带有hashtag的推文

Use*_*sss 6 python twitter twython

我们如何根据哈希标记查找或获取推文.即我想找到关于某一主题的推文?是否可以在Python中使用Twython?

谢谢

Ben*_*ite 19

编辑 我使用Twython的搜索API钩子的原始解决方案似乎不再有效,因为Twitter现在希望用户通过身份验证来使用搜索.要通过Twython进行经过身份验证的搜索,只需在初始化Twython对象时提供Twitter身份验证凭据.下面,我将粘贴一个如何执行此操作的示例,但您需要查阅GET/search/tweets的Twitter API文档,以了解您可以在搜索中指定的不同可选参数(例如,到页面)通过结果,设置日期范围等)

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'
Run Code Online (Sandbox Code Playgroud)

原始答案

Twython文档中所示,您可以使用Twython访问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"
Run Code Online (Sandbox Code Playgroud)

等等...请注意,对于任何给定的搜索,您最多可能最多大约2000条推文,最多可以回到一两周左右.您可以在此处阅读有关Twitter Search API的更多信息.