处理Tweepy api返回的420响应代码

2024-04-20 10:57:42 发布

您现在位置:Python中文网/ 问答频道 /正文

每当用户登录到我的应用程序并进行搜索时,我都必须启动一个流式API来获取他所需的数据。在

这是我的stream 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流媒体

^{pr2}$

但如果我点击两次以上我的应用程序返回错误代码420。 我想更改API(使用多个键)用于在发生错误420时获取数据。在

如何获取def callStream()类的on-error方法引起的错误


Tags: thetoimportselfapijson应用程序data
2条回答

根据Twittererror response code documentation

Returned when an application is being rate limited for making too many requests.

Twitter流媒体API不支持每个用户和IP地址超过两个连接。使用多个应用程序密钥试图绕过这一点是违反Twitter开发者政策的,如果你这样做,你的应用程序可能会被暂停。在

我想补充一下@Andy Piper的回答。响应420意味着您的脚本发出的请求太多,并且速率受到限制。要解决这个问题,我要做的是(课堂上的TweetListener):

def on_limit(self,status):
    print ("Rate Limit Exceeded, Sleep for 15 Mins")
    time.sleep(15 * 60)
    return True

这样做,错误就会得到处理。在

如果你坚持使用多个键。我不确定,但尝试在TweetListener和streamer上进行异常处理,因为tweepy.error.RateLimitError公司并使用下一个API密钥对函数进行递归调用?在

^{pr2}$

相关问题 更多 >