Python-Twitter:获取最新提及
我正在尝试使用Python-Twitter库(https://github.com/bear/python-twitter)来提取某个推特账号的提及,使用的是GetMention()这个函数。这个脚本会定期运行,并把数据存入数据库,所以我不想提取每一个提及,只想要自上次运行脚本以来的新提及。
下面的代码能够正常提取提及,但不知为何,'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
你的语法看起来和Python-Twitter库里提到的一致。我觉得可能发生的情况是这样的:
如果在你指定的since_id之后,推文的数量达到了限制,那么since_id会被强制设置为最旧的可用ID。
这就意味着你会看到所有推文都是从最旧的可用ID开始的。试着用一个更新的since ID值来进行操作。同时,也要检查一下你提供的since ID是否合适。