python - 使用 Python 从 Twitter 获取带有标签的推文

标签 python twitter twython

<分区>

我们如何根据散列标签查找或获取推文。即我想找到关于某个主题的推文?是否可以在 Python 中使用 Twython?

谢谢

最佳答案

编辑 我最初使用 Twython 的搜索 API Hook 的解决方案似乎不再有效,因为 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'

原始答案

如图所示here in the Twython documentation ,您可以使用 Twython 访问 Twitter 搜索 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"

等等...请注意,对于任何给定的搜索,您最多可能最多只能搜索大约 2000 条推文,然后再回溯到大约一两周。您可以阅读有关 Twitter 搜索 API 的更多信息 here .

关于python - 使用 Python 从 Twitter 获取带有标签的推文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14156625/

相关文章:

python - 是否可以使用 ScyllaDB for python 更有效地添加数据?

python - 如何连接列值在一定范围内的两个数据框?

python - 是否可以在 Flutter 应用程序中运行 Python 解释器?

java - 使用 tweet4j 按位置获取推文

twitter - Twitter 的速率限制是否允许我进行必要的数据挖掘以构建一个大约 60 万用户的完整社交网络图?

python - Twython - 如何使用媒体 url 更新状态

python - 表 "column"中有一个名为 "table"的列,但无法从这部分查询中引用它

python - 将python字符串转换成json格式

twitter - 由于 Twitter 未返回 'next_results',因此从时间线获取推文时出现问题

python - 使用 Twython 在 Twitter 上自动关注