【发布时间】:2018-10-21 17:10:37
【问题描述】:
每当用户登录我的应用程序并进行搜索时,我都必须启动一个流式 API 来获取他所需的数据。
这是我的流 API 类
import tweepy
import json
import sys
class TweetListener(tweepy.StreamListener):
def on_connect(self):
# Called initially to connect to the Streaming API
print("You are now connected to the streaming API.")
def on_error(self, status_code):
# On error - if an error occurs, display the error / status code
print('An Error has occured: ' + repr(status_code))
return False
def on_data(self, data):
json_data = json.loads(data)
print(json_data)
这是我的 python 代码文件,它调用上面的类来启动 Twitter 流
import tweepy
from APIs.StreamKafkaApi1 import TweetListener
consumer_key = "***********"
consumer_secret = "*********"
access_token = "***********"
access_secret = "********"
hashtags = ["#ipl"]
def callStream():
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth,wait_on_rate_limit=True)
tweetListener = TweetListener(userid,projectid)
streamer = tweepy.Stream(api.auth, tweetListener)
streamer.filter(track=hashtags, async=True)
if __name__ == "__main__":
callStream()
但如果我的应用程序点击了两次以上返回错误代码 420。 我想更改 API(使用多个键) 用于在发生错误 420 时获取数据。
如何获取 中 TweetListener 类的 on_error 方法引发的错误def callStream()
【问题讨论】:
标签: python twitter error-handling tweepy twitter-streaming-api