【问题标题】:Writing a notification program using Tweepy使用 Tweepy 编写通知程序
【发布时间】:2012-05-15 14:56:33
【问题描述】:

在尝试学习 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,我的帐户已通过身份验证。

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

我原以为会有这样的代码:

api.set_home_timeline_handler(tweetHandler)

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

【问题讨论】:

    标签: python twitter tweepy


    【解决方案1】:

    这是 twitter API 的工作方式 - 每次您想了解 twitter 上的内容时都会询问。但要注意速率限制https://dev.twitter.com/docs/rate-limiting 每 10 秒超过最大值。

    另外还有流式 API:https://dev.twitter.com/docs/streaming-api/methods,它的工作方式与您想象的一样,但它用于比检查时间线更大的任务。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-08
      • 1970-01-01
      • 1970-01-01
      • 2012-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多