gpt4 book ai didi

python - 使用 tweepy 直播推文

转载 作者:太空宇宙 更新时间:2023-11-04 00:44:42 24 4
gpt4 key购买 nike

我正在使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com