gpt4 book ai didi

python - Twitter API 搜索困难

转载 作者:行者123 更新时间:2023-11-28 18:48:33 25 4
gpt4 key购买 nike

我是 python Twitter API 的新手。
我已经在 Twitter 上设置了身份验证,我尝试了搜索,效果很好。

t = twitter.Twitter(auth=auth)    
tt = t.search.tweets(q="stackoverflow",rpp=50)

这工作正常,我可以读取输出。

但如果我尝试:

for page in range(1,6):
tt = t.search.tweets(q="stackoverflow", rpp=100, page=page)

这会因错误而崩溃:

details: {"errors":[{"code":44,"message":"page parameter is invalid"}]}

我查看了 Twitter 页面,页面参数似乎可以作为可选参数:https://dev.twitter.com/docs/api/1/get/search .

或者,如果每个请求的速率限制为 100 个结果,我如何获得更多搜索结果。是否有其他解决方法?

最佳答案

您应该使用 max_id 值来遍历结果。向 Twitter API 发出第一个请求,从结果中读取 max_id 并在您的下一个请求中发送它。

来自docs :

To use max_id correctly, an application’s first request to a timeline endpoint should only specify a count. When processing this and subsequent responses, keep track of the lowest ID received. This ID should be passed as the value of the max_id parameter for the next request, which will only return Tweets with IDs lower than or equal to the value of the max_id parameter. Note that since the max_id parameter is inclusive, the Tweet with the matching ID will actually be returned again, as shown in the following image:

enter image description here

使用Twython :

from twython import Twython

twitter = Twython(APP_KEY, access_token=ACCESS_TOKEN)

# Initial request
result = twitter.search(q='stackoverflow', count=100)

# Get max_id of results
next_max_id = result['search_metadata']['next_results'].split('max_id=')[1].split('&')[0]
# Use max_id in next request
result = twitter.search(q='stackoverflow', count=100, max_id=next_max_id)

关于python - Twitter API 搜索困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16514479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com