【问题标题】:tweepy: get all mentions with api.search using max_id and since_idtweepy:使用 api.search 使用 max_id 和 since_id 获取所有提及
【发布时间】:2018-09-09 14:19:21
【问题描述】:

我在这里关注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 &lt; 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 中的推文所说的任何内容的推文。 所以,总结一下,我的问题是:

  1. 我可以用while True 代替while tweetCount &lt; maxTweets 吗?如果不能,为什么?
  2. 我解释函数的方式是否正确,如果不正确,我哪里出错了?
  3. max_id = new_tweets[-1].id 究竟做了什么?
  4. 为什么我们不在 for 循环中将 sinceId 设置为新值?由于一开始就将sinceId设置为None,如果我们不更改任何地方的值,似乎没有必要通过sinceId不设置为None的选项。

作为免责声明:我确实阅读了 Twitter explantion 对 max_id、since_id、计数等的解释,但它没有回答我的问题。

【问题讨论】:

    标签: python twitter tweepy


    【解决方案1】:

    几个月前,我对 Search API 使用了相同的参考。我开始了解一些可能对您有所帮助的事情。我假设 API 以有序的方式返回推文(tweet_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

    您可以这样做,但您需要处理因达到速率限制(即每次调用 100 条推文)而导致的异常。使用它可以更轻松地处理程序。

    我希望这对你有帮助。

    【讨论】:

      【解决方案2】:

      我可以用 while True 代替 while tweetCount

      我使用 Twitter API 已经有一段时间了,但如果我没记错的话,你在一小时内收到的电话和推文数量是有限的。这是为了保持 Twitter 相对干净。我记得 maxTweets 应该是您想要获取的数量。这就是您可能不想使用while True 的原因,但我相信您可以毫无问题地替换它。您最终会遇到一个异常,即 API 告诉您您已达到最大数量。

      max_id = new_tweets[-1].id 到底是做什么的?

      每条推文都有一个 ID,这是您在打开它时在 URL 中看到的那个。您可以使用它来引用代码中的特定推文。该代码所做的是将返回列表中最后一条推文的 ID 更新为您最后一条推文的 ID。 (基本上更新变量)。请记住调用负索引是指从列表末尾开始向后的元素。

      我不是 100% 确定您的其他两个问题,如果我发现任何问题,我稍后会进行编辑。

      【讨论】:

        猜你喜欢
        • 2012-06-03
        • 2012-03-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-13
        • 1970-01-01
        • 2015-12-07
        相关资源
        最近更新 更多