python - 使用 tweepy 进行推文直播

标签 python twitter tweepy live-streaming

我正在使用 Tweepy 访问流 API 并将其存储在 JSON 文件中。 代码看起来不错,但我不断收到此错误: AttributeError:“NoneType”对象没有属性“strip”

import sys
import tweepy
import json

consumer_key = 'XXXX'
consumer_secret = 'XXXX'
access_key = 'XXXX'
access_secret = 'XXXX'

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
file = open('today.txt', 'a')

class CustomStreamListener(tweepy.StreamListener):
    def on_status(self, status):
        print (status.text)

    def on_data(self, data):
        json_data = json.loads(data)
        file.write(str(json_data))

    def on_error(self, status_code):
        print >> sys.stderr, 'Encountered error with status code:', status_code
        return True # Don't kill the stream

    def on_timeout(self):
        print >> sys.stderr, 'Timeout...'
        return True # Don't kill the stream

sapi = tweepy.streaming.Stream(auth, CustomStreamListener())
sapi.filter(track=['twitter'])

这里的错误在最后一行。 我认为流返回 null,因此当我尝试过滤它时它会给出错误。 请帮忙!

最佳答案

自 10 月 18 日起,Tweepy 将不再维护,但这不是问题的原因。

自 10 月 21 日 DYN 遭受攻击以来,twitter 流 API 一直遇到问题。这些已在 Twitter 开发者论坛上报告,并且 Twitter 工作人员已意识到该问题:https://twittercommunity.com/t/issues-reported-with-streams-since-10-21/76429 .

到目前为止,对于问题的原因还没有明确的答案,但观察到的行为是,与任何 Twitter 流媒体端点的连接都会在短时间内关闭,通常(尽管并非总是)没有接收任何数据。有时,连接会在发送响应时关闭,从而导致无效的 JSON。

关于python - 使用 tweepy 进行推文直播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40230330/

相关文章:

python - 刷新页面时取消POST

python - Django - 没有这样的表 : main. auth_user__old

php - mysqli_query 上的 undefined variable

python - 如何根据列表值的重复性拆分列表列表?

javascript - 使用 passportjs 完成本地用户身份验证后,将 Twitter 帐户与 Passportjs 关联起来

javascript - 我向 Twitter API 发出了请求。如何获取从 PHP 脚本到 JavaScript 的 JSON 响应?

python - 查看哪个 Python 类正在使用我的 100% CPU?

python - 使用推文 ID 使用 tweepy 查找推文在几条推文后停止 - 用户暂停错误

python - 跟踪哪个(tweepy)过滤器捕获了一条推文

python - 如何根据特定的键值对比较两个 pandas 系列?