1

在尝试学习 Python-tweepy api 时,我整理了一些代码,告诉我在最近的约 10 秒间隔内是否有任何新推文(当然,实际上它超过 10 秒,因为代码除了sleep(10)还需要一些时间来运行):

from getApi import getApi
from time import sleep
from datetime import datetime

api = getApi()
top = api.home_timeline()[0].id

while True:
    l = api.home_timeline()
    if top != l[0].id:
        top = l[0].id
        print 'New tweet recognized by Python at: %s' % str(datetime.now())
    sleep(10)

getApi 只是我编写的几行 Python 代码,用于使用 tweepy 管理 OAuth。getApi 方法返回 tweepy api,我的帐户已通过身份验证。

我不得不一次又一次地询问推特是否有任何新推文,这似乎非常低效。这是通常的做法吗?如果不是,那么“规范”的做法是什么?

我本来以为会有一些代码,例如:

api.set_home_timeline_handler(tweetHandler)

只要有新推文,就会调用 tweetHandler。

4

1 回答 1

1

这就是 twitter API 的工作方式——每次你想知道 twitter 上的东西时,你都会问。https://dev.twitter.com/docs/rate-limiting但要注意每 10 秒的速率限制高于最大值。

或者,还有流 api:https://dev.twitter.com/docs/streaming-api/methods它可以像您想象的那样工作,但它用于比检查时间线更大的任务。

于 2012-05-07T06:46:42.690 回答