简体   繁体   English

如何使用 Twitter 搜索 API 获取具有给定主题标签的所有推文?

[英]How to fetch all tweets with a given hashtag using the Twitter search API?

First of all let me note that this is the first time I am using the Twitter API, so I could be missing something obvious.首先让我注意,这是我第一次使用 Twitter API,所以我可能会遗漏一些明显的东西。

What I want to do is get all tweets that include a given hashtag.我想要做的是获取包含给定主题标签的所有推文。 My research lead me to use the twitter search API.我的研究使我使用了 Twitter 搜索 API。 I've tried using it, however I only seem to get about 6 tweets, when I know that the hashtag has thousands of tweets.我试过使用它,但是当我知道主题标签有数千条推文时,我似乎只收到了大约 6 条推文。

So my question is, how do I actually get all of the tweets for a given hashtag?所以我的问题是,我如何实际获取给定主题标签的所有推文? Or at least more than 6 tweets.或至少超过 6 条推文。

For reference, there is the python code I am using to fetch tweets with the #hillarysoqualified hashtag (replace the keys obviously):作为参考,我使用 python 代码来获取带有 #hillarysoqualified 主题标签的推文(显然替换键):

from twitter import Twitter, OAuth                                                                                                                                                                                                         

ACCESS_TOKEN = 'access_token'
ACCESS_SECRET = 'access_secret'
CONSUMER_KEY = 'consumer_key'
CONSUMER_SECRET = 'consumer_secret'

oauth = OAuth(ACCESS_TOKEN, ACCESS_SECRET, CONSUMER_KEY, CONSUMER_SECRET)
t = Twitter(auth=oauth)

query = t.search.tweets(q='%23hillarysoqualified')

for s in query['statuses']:
    print(s['created_at'], s['text'], '\n')

It appears I had not read the docs - Twitter search API only gives you tweets from the last week.看来我没有阅读文档 - Twitter 搜索 API 只为您提供上周的推文。 Hope this helps if someone else tries to do what I did without knowing.如果其他人在不知情的情况下尝试做我所做的事情,希望这会有所帮助。

yes, the standard search API gives you only access to last 7 days, but you request an access to a premium Search API sandbox for free which gives access to up to 30 days.是的,标准搜索 API 只允许您访问过去 7 天,但您请求免费访问高级搜索 API 沙盒,最多可访问 30 天。 you can find more information here https://developer.twitter.com/en/docs/tweets/search/overview您可以在此处找到更多信息https://developer.twitter.com/en/docs/tweets/search/overview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM