Python-Twitter:检索最近的提及

时间:2014-08-12 22:42:47

标签: python twitter python-twitter

我尝试使用Python-Twitter库(https://github.com/bear/python-twitter)使用GetMention()函数提取twitter帐户的提及。该脚本填充数据库并在cron作业上定期运行,因此我不想提取每一个提及,只提取自上次运行脚本以来的提及。

以下代码提取的内容很好,但出于某种原因,' since_id'参数似乎没有做任何事情 - 该函数每次运行时都返回所有提及,而不是仅针对最近的提及进行过滤。作为参考,文档在这里:https://python-twitter.googlecode.com/hg/doc/twitter.html#Api-GetMentions

实现GetMention()函数的正确方法是什么? (我看过,但我无法在网上找到任何例子)。或者,是否有一种不同/更优雅的方式来提取我忽略的推文?

def scan_timeline():
''' Scans the timeline and populates the database with the results '''

    FN_NAME = "scan_timeline"

    # Establish the api connection
    api = twitter.Api(
                  consumer_key = "consumerkey",
                  consumer_secret = "consumersecret",
                  access_token_key = "accesskey",
                  access_token_secret = "accesssecret"
                  )


    # Tweet ID of most recent mention from the last time the function was run
    # (In actual code this is dynamic and extracted from a database)
    since_id = 498404931028938752

    # Retrieve all mentions created since the last scan of the timeline
    length_of_response = 20
    page_number = 0

    while length_of_response == 20:

        # Retreive most recent mentions
        results = api.GetMentions(since_id,None,page_number)


    ### Additional code inserts the tweets into a database ###

1 个答案:

答案 0 :(得分:0)

您的语法似乎与Python-Twitter库中提到的一致。我认为发生的事情如下:

  

如果自Since_id以来发生了推文限制,则since_id将被强制使用最旧的ID。

这会导致所有推文都从最早的可用ID开始。尝试使用更新的ID值。同样地,还要检查您提供的ID是否合适。

相关问题