python - tweepy:使用 api.search 使用 max_id 和 since_id 获取所有提及
问题描述
我在这里点击了这个链接来获取所有提到某个查询的推文。现在,代码到目前为止运行良好,我只是想确保我真正理解任何东西,因为我不想使用某些代码,即使我什至不知道它是如何做的。这是我的相关代码:
def searchMentions (tweetCount, maxTweets, searchQuery, tweetsPerQry, max_id, sinceId) :
while tweetCount < maxTweets:
if (not max_id):
if (not sinceId):
new_tweets = api.search(q=searchQuery, count=tweetsPerQry)
else:
new_tweets = api.search(q=searchQuery, count = tweetsPerQry, since_id = sinceId)
else:
if (not sinceId):
new_tweets = api.search(q=searchQuery, count= tweetsPerQry, max_id=str(max_id -1))
else:
new_tweets = api.search(q=searchQuery, count=tweetsPerQry, max_id=str(max_id -1), since_id=sinceId)
if not new_tweets:
print("No new tweets to show")
break
for tweet in new_tweets :
try :
tweetCount += len(new_tweets)
max_id = new_tweets[-1].id
tweetId = tweet.user.id
username = tweet.user.screen_name
api.update_status(tweet.text)
print(tweet.text)
except tweepy.TweepError as e:
print(e.reason)
except StopIteration:
pass
我假设 max_id 和 sinceId 都设置为 None 因为尚未找到任何推文。tweetCount 设置为零。我理解它的方式是 while 循环运行 while tweetCount < maxTweets
。例如,我不确定为什么会这样以及为什么我不能使用while True
。起初我认为这可能与 api 调用的速率有关,但这并没有真正的意义。
之后,该函数检查 max_id 和 sinceId。我假设它检查是否已经有一个 max_id,如果 max_id 没有,它会检查sinceId。如果 sinceId 为 none,那么它只会获取 count 参数设置为多少条推文,否则它将下限设置为 sinceId 并获取多少条推文,count 参数设置为从 sinceId 开始。如果 max_id 不是 none,但如果 sinceId 设置为 none,它将上限设置为 max_id 并获取一定数量的推文,直到并包括该界限。因此,如果您有 id 为 1、2、3、4、5 且 count=3 和 max_id=5 的推文,您将获得 3、4、5 的推文。否则,它将下限设置为 sinceId 并将上限设置为 max_id 并获取“介于两者之间”的推文。找到的推文保存在 new_tweets 中。
现在,该函数遍历 new_tweets 中的所有推文,并将 tweetCount 设置为该列表的长度。然后 max_id 设置为new_tweets[-1].id
. 由于 twitter 指定 max_id 是包容性的,我假设这被设置为最后一条推文之前的下一条推文,所以推文不会重复,但是,我不太确定,我不明白我的函数如何知道什么最后一条推文之前的 id 可能是。发布一条重复 new_tweets 中的推文所说的任何内容的推文。所以,总结一下,我的问题是:
- 我可以
while True
代替while tweetCount < maxTweets
吗?如果不可以,为什么? - 我解释功能的方式是否正确,如果不正确,我哪里出错了?
- 具体做什么
max_id = new_tweets[-1].id
? - 为什么我们不在 for 循环中将 sinceId 设置为新值?由于 sinceId 一开始就设置为 None,如果我们不在任何地方更改值,似乎没有必要通过 sinceId 不设置为 None 的选项。
作为免责声明:我确实阅读了 Twitter对 max_id、 since_id、counts 等的解释,但它没有回答我的问题。
解决方案
几个月前,我对 Search API 使用了相同的参考。我开始了解一些可能对您有所帮助的事情。我假设 API 以有序的方式返回推文(推文 ID 的降序)。
假设我们有一堆推文,推特给我们一个查询,推文 ID 从 1 到 10(1 是最旧的,10 是最新的)。
1 2 3 4 5 6 7 8 9 10
since_id = 下限和 max_id = 上限
Twitter 开始按照最新到最旧的顺序(从 10 到 1)返回推文。让我们举一些例子:
# This would return tweets having id between 4 and 10 ( 4 and 10 inclusive )
since_id=4,max_id=10
# This means there is no lower bound, and we will receive as many
# tweets as the Twitter Search API permits for the free version ( i.e. for the last 7
# days ). Hence, we will get tweets with id 1 to 10 ( 1 and 10 inclusive )
since_id=None, max_id=10
max_id = new_tweets[-1].id 究竟做了什么?
假设在第一个 API 调用中我们只收到了 4 条推文,即 10、9、8、7。因此,new_tweets 列表变为(出于解释的目的,我假设它是一个 id 列表,它实际上是一个对象):
new_tweets=[10,9,8,7]
max_id= new_tweets[-1] # max_id = 7
现在,当我们的程序第二次访问 API 时:
max_id = 7
since_id = None
new_tweets = api.search(q=searchQuery, count=tweetsPerQry, max_id=str(max_id -1), since_id=sinceId)
# We will receive all tweets from 6 to 1 now.
max_id = 6 # max_id=str(max_id -1)
#Therefore
new_tweets = [6,5,4,3,2,1]
对于我们进行的每个 API 调用,这种使用 API 的方式(如参考资料中所述)最多可以返回 100 条推文。返回的实际推文数量少于 100 条,并且还取决于您的查询的复杂程度,越不复杂越好。
为什么我们不在 for 循环中将 sinceId 设置为新值?由于 sinceId 一开始就设置为 None,如果我们不在任何地方更改值,似乎没有必要通过 sinceId 不设置为 None 的选项。
设置 sinceId=None 会返回最旧的推文,但如果我们不提及它,我不确定 sinceId 的默认值是什么。
我可以用 while True 代替 while tweetCount < maxTweets 吗?如果不能,为什么?
您可以这样做,但是您需要处理因达到速率限制(即每次调用 100 条推文)而导致的异常。使用它可以更轻松地处理程序。
我希望这可以帮助你。
推荐阅读
- c# - 检查是否使用带有 Wix 的 CustomAction 安装了 .NETCore
- oracle - 错误 ORA-01722:创建实体化视图时编号无效(Oracle SDE 问题?)
- visual-studio-code - 有什么方法可以直观地更改 VS Code 中特定编辑器的表示?
- html - 我提交 html 表单时出现“about:blank#blocked”错误
- c - 使用支持 FIPS 的 openssl 编译应用程序
- php - 发送电子邮件时出错 [Symfony mailerBundle]
- java - Camunda 用于存储数据
- javascript - 无法获取要在单元格中链接的日历事件 ID
- spring-boot - 带有小胡子和弹簧错误标签的错误消息
- javascript - 在 vue 中使用 jest 时无法编译但测试用例通过