我正在尝试编写自己的 twitter-fetch api,基于 python-twitter图书馆。目标是获取 Donald Trump 的最后 5 条推文。我每隔一秒左右就会收到不同数量的推文。有时是 5 条推文,有时只有 1 条。
import twitter
api = twitter.Api(consumer_key=config["credentials"]["key"],
consumer_secret=config["credentials"]["secret"],
application_only_auth=True,
tweet_mode='extended')
tweets = api.GetUserTimeline(
screen_name="realDonaldTrump",
count=5)
print("number of tweets: {}".format(len(tweets)))
之前有人在使用其他语言/包的 twitter REST api 中注意到过这一点吗?
之前有没有人注意到 python-twitter 的这一点?我正在使用的包?
最佳答案
这是正常行为。请参阅本文档中的count
:https://developer.twitter.com/en/docs/tweets/timelines/api-reference/get-statuses-user_timeline.html
count
指定最大限制,而不是 promise 发送指定数量的推文。 Twitter 可能会因缺乏相关性等原因从您请求的 5 条推文中剔除一些。
对于您的场景,只需请求多于 5 条(例如 100 条)的推文,并仅使用前 5 条。
关于python twitter api 每秒返回的推文数量不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55125650/