tweepy:使用 max_id 和 since_id 获取所有提及 api.search 的内容
tweepy: get all mentions with api.search using max_id and since_id
我在此处关注 this link 以获取所有提及特定查询的推文。
现在,代码到目前为止工作正常,我只是想确保我真正理解任何东西,因为我不想使用某些代码,即使我什至不知道它是如何做的。
这是我的相关代码:
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 循环在 tweetCount < maxTweets
时运行。例如,我不确定为什么会这样以及为什么我不能使用 while True
。起初我认为这可能与 api 呼叫率有关,但这并没有真正意义。
之后,函数检查 max_id 和 sinceId。我假设它会检查是否已经存在 max_id,如果 max_id 是 none,它会检查 sinceId。如果 sinceId 是 none 那么它只是获取 count 参数设置的推文数量,否则它将下限设置为 sinceId 并从 sinceId 开始获取 count 参数设置的推文数量。
如果 max_id 不是 none,但如果 sinceId 设置为 none,它会将上限设置为 max_id 并获取一定数量的推文,直到并包括该界限。因此,如果您有 ID 为 1、2、3、4、5 且计数为 3 且 max_id=5 的推文,您将获得推文 3、4、5。否则它将下限设置为 sinceId 并将上限设置为 max_id 并获取推文 "in between"。
找到的推文保存在 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 explantion 对 max_id、since_id、计数等的解释,但它没有回答我的问题。
Can I do while True instead of while tweetCount < maxTweets and if not, why?
我已经有一段时间没有使用 Twitter API 但如果我没记错的话,你在一个小时内的电话和推文数量有限。这是为了保持 Twitter 相对干净。我记得 maxTweets 应该是你想要获取的数量。这就是为什么您可能不想使用 while True
,但我相信您可以毫无问题地替换它。您最终会遇到一个例外,即 API 告诉您您已达到最大金额。
What does max_id = new_tweets[-1].id do exactly?
每条推文都有一个 ID,就是您打开它时在 URL 中看到的 ID。您可以使用它来引用代码中的特定推文。该代码的作用是将返回列表中最后一条推文的 ID 更新为您最后一条推文的 ID。 (基本上更新变量)。请记住调用负索引是指从列表末尾开始向后的元素。
我不是 100% 确定你的其他两个问题,如果我发现任何问题,我会稍后编辑。
几个月前,我使用相同的参考文献进行搜索 API。我开始了解一些可能对您有帮助的事情。我假设 API return 的推文是有序的(降序 tweet_id)。
假设我们有一堆推文,twitter 给我们一个查询,推文 ID 从 1 到 10(1 是最旧的,10 是最新的)。
1 2 3 4 5 6 7 8 9 10
since_id = 下限和
max_id = 上限
Twitter 开始 return 按从新到旧(从 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
What does max_id = new_tweets[-1].id do exactly?
假设在第一个 API 调用中我们只收到 4 条推文,即 10、9、8、7。因此,new_tweets 列表变为(我假设它是一个列表ids 用于解释,它实际上是一个对象列表):
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 的方法(如参考资料中所述)可以 return 最多 100 条推文,对于我们进行的每个 API 调用。推文的实际数量 returned 少于 100 并且还取决于您的查询的复杂程度,越不复杂越好。
Why do we not set sinceId to a new value in the for-loop? Since sinceId is set to None in the beginning, it seems unnecessary to go through the options of sinceId not being set to None if we do not change the value anywhere.
设置 sinceId=None returns 最旧的推文,但我不确定 sinceId 的默认值是多少,如果我们不提及的话。
Can I do while True instead of while tweetCount < maxTweets and if not, why?
您可以这样做,但是您需要处理达到速率限制(即每次调用 100 条推文)时出现的异常。使用它可以使程序的处理更容易。
希望对您有所帮助。
我在此处关注 this link 以获取所有提及特定查询的推文。 现在,代码到目前为止工作正常,我只是想确保我真正理解任何东西,因为我不想使用某些代码,即使我什至不知道它是如何做的。 这是我的相关代码:
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 循环在 tweetCount < maxTweets
时运行。例如,我不确定为什么会这样以及为什么我不能使用 while True
。起初我认为这可能与 api 呼叫率有关,但这并没有真正意义。
之后,函数检查 max_id 和 sinceId。我假设它会检查是否已经存在 max_id,如果 max_id 是 none,它会检查 sinceId。如果 sinceId 是 none 那么它只是获取 count 参数设置的推文数量,否则它将下限设置为 sinceId 并从 sinceId 开始获取 count 参数设置的推文数量。 如果 max_id 不是 none,但如果 sinceId 设置为 none,它会将上限设置为 max_id 并获取一定数量的推文,直到并包括该界限。因此,如果您有 ID 为 1、2、3、4、5 且计数为 3 且 max_id=5 的推文,您将获得推文 3、4、5。否则它将下限设置为 sinceId 并将上限设置为 max_id 并获取推文 "in between"。 找到的推文保存在 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 explantion 对 max_id、since_id、计数等的解释,但它没有回答我的问题。
Can I do while True instead of while tweetCount < maxTweets and if not, why?
我已经有一段时间没有使用 Twitter API 但如果我没记错的话,你在一个小时内的电话和推文数量有限。这是为了保持 Twitter 相对干净。我记得 maxTweets 应该是你想要获取的数量。这就是为什么您可能不想使用 while True
,但我相信您可以毫无问题地替换它。您最终会遇到一个例外,即 API 告诉您您已达到最大金额。
What does max_id = new_tweets[-1].id do exactly?
每条推文都有一个 ID,就是您打开它时在 URL 中看到的 ID。您可以使用它来引用代码中的特定推文。该代码的作用是将返回列表中最后一条推文的 ID 更新为您最后一条推文的 ID。 (基本上更新变量)。请记住调用负索引是指从列表末尾开始向后的元素。
我不是 100% 确定你的其他两个问题,如果我发现任何问题,我会稍后编辑。
几个月前,我使用相同的参考文献进行搜索 API。我开始了解一些可能对您有帮助的事情。我假设 API return 的推文是有序的(降序 tweet_id)。
假设我们有一堆推文,twitter 给我们一个查询,推文 ID 从 1 到 10(1 是最旧的,10 是最新的)。
1 2 3 4 5 6 7 8 9 10
since_id = 下限和 max_id = 上限
Twitter 开始 return 按从新到旧(从 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
What does max_id = new_tweets[-1].id do exactly?
假设在第一个 API 调用中我们只收到 4 条推文,即 10、9、8、7。因此,new_tweets 列表变为(我假设它是一个列表ids 用于解释,它实际上是一个对象列表):
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 的方法(如参考资料中所述)可以 return 最多 100 条推文,对于我们进行的每个 API 调用。推文的实际数量 returned 少于 100 并且还取决于您的查询的复杂程度,越不复杂越好。
Why do we not set sinceId to a new value in the for-loop? Since sinceId is set to None in the beginning, it seems unnecessary to go through the options of sinceId not being set to None if we do not change the value anywhere.
设置 sinceId=None returns 最旧的推文,但我不确定 sinceId 的默认值是多少,如果我们不提及的话。
Can I do while True instead of while tweetCount < maxTweets and if not, why?
您可以这样做,但是您需要处理达到速率限制(即每次调用 100 条推文)时出现的异常。使用它可以使程序的处理更容易。
希望对您有所帮助。