我在此处点击此链接以获取所有提及某个查询的推文。现在,代码到目前为止运行良好,我只是想确保我真的理解了任何东西,因为我不想使用某些代码,即使我什至不知道它是如何做的。这是我的相关代码:
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
?作为免责声明:我确实通读了推特对 max_id、since_id、counts 等的解释说明,但它没有回答我的问题。
几个月前,我对 Search API 使用了相同的参考。我开始了解一些可能对您有所帮助的事情。我假设 API 以有序的方式返回推文(tweet_id 的降序)。
假设我们有一堆推文,推特给我们一个查询,推文 ID 从 1 到 10(1 是最旧的,10 是最新的)。
1 2 3 4 5 6 7 8 9 10
因为_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,如果我们不在任何地方更改该值,似乎没有必要通过未设置为None 的sinceId 选项。
设置 sinceId=None 返回最旧的推文,但如果我们不提及它,我不确定 sinceId 的默认值是什么。
我可以做 while True 而不是 while tweetCount < maxTweets 如果不能,为什么?
您可以这样做,但您随后需要处理因达到速率限制(即每次调用 100 条推文)而导致的异常。使用它可以更容易地处理程序。
我希望这可以帮助你。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句