twitter API 如何用于流式推文?错误 420
How the twitter API works for streaming tweets ? Error 420
我尝试使用 Twitter Streaming 摄取推文 API。
昨天,经过多次测试,Twitter API 返回错误 420。我阅读了一些主题和文档,问题是我在短时间内建立了很多连接。
from tweepy import Stream, API
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import json
# All API keys / access token
consumer_key = "something"
consumer_secret_key = "something"
access_token = "something"
access_token_secret = "something"
proxies = {
"http": "my_http_proxy",
"https": "my_https_proxy"
}
class Listener(StreamListener):
def on_status(self, status):
print("text : " + str(status))
def on_error(self, status):
if status == 420:
print("error : {}".format(str(status)))
return False
auth = OAuthHandler(consumer_key, consumer_secret_key)
auth.set_access_token(access_token, access_token_secret)
api = API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
listener = Listener()
twitterStream = Stream(api.auth, listener=listener, proxies=proxies)
try:
twitterStream.filter(track=['nasa'])
except Exception as e:
print("...end : {}".format(e))
twitterStream.disconnect()
twitterStream.disconnect()
我想了解一下:
- 如何避免此错误?
- 我需要等待多长时间才能再次请求 API?
- 这个 API 标准账户的限价是多少?
非常感谢您的回复。
Twitter 的 API returns 420 HTTP 状态代码
when an app is being rate limited for making too many requests.
参见https://developer.twitter.com/en/docs/basics/response-codes。
具体来说,对于流媒体端点:
Back off exponentially for HTTP 420 errors. Start with a 1 minute wait and double each attempt. Note that every HTTP 420 received increases the time you must wait until rate limiting will no longer will be in effect for your account.
Clients which do not implement backoff and attempt to reconnect as often as possible will have their connections rate limited for a small number of minutes. Rate limited clients will receive HTTP 420 responses for all connection requests.
Clients which break a connection and then reconnect frequently (to change query parameters, for example) run the risk of being rate limited.
Twitter does not make public the number of connection attempts which will cause a rate limiting to occur, but there is some tolerance for testing and development. A few dozen connection attempts from time to time will not trigger a limit. However, it is essential to stop further connection attempts for a few minutes if a HTTP 420 response is received. If your client is rate limited frequently, it is possible that your IP will be blocked from accessing Twitter for an indeterminate period of time.
见https://developer.twitter.com/en/docs/tweets/filter-realtime/guides/connecting。
我尝试使用 Twitter Streaming 摄取推文 API。
昨天,经过多次测试,Twitter API 返回错误 420。我阅读了一些主题和文档,问题是我在短时间内建立了很多连接。
from tweepy import Stream, API
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import json
# All API keys / access token
consumer_key = "something"
consumer_secret_key = "something"
access_token = "something"
access_token_secret = "something"
proxies = {
"http": "my_http_proxy",
"https": "my_https_proxy"
}
class Listener(StreamListener):
def on_status(self, status):
print("text : " + str(status))
def on_error(self, status):
if status == 420:
print("error : {}".format(str(status)))
return False
auth = OAuthHandler(consumer_key, consumer_secret_key)
auth.set_access_token(access_token, access_token_secret)
api = API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
listener = Listener()
twitterStream = Stream(api.auth, listener=listener, proxies=proxies)
try:
twitterStream.filter(track=['nasa'])
except Exception as e:
print("...end : {}".format(e))
twitterStream.disconnect()
twitterStream.disconnect()
我想了解一下:
- 如何避免此错误?
- 我需要等待多长时间才能再次请求 API?
- 这个 API 标准账户的限价是多少?
非常感谢您的回复。
Twitter 的 API returns 420 HTTP 状态代码
when an app is being rate limited for making too many requests.
参见https://developer.twitter.com/en/docs/basics/response-codes。
具体来说,对于流媒体端点:
Back off exponentially for HTTP 420 errors. Start with a 1 minute wait and double each attempt. Note that every HTTP 420 received increases the time you must wait until rate limiting will no longer will be in effect for your account.
Clients which do not implement backoff and attempt to reconnect as often as possible will have their connections rate limited for a small number of minutes. Rate limited clients will receive HTTP 420 responses for all connection requests.
Clients which break a connection and then reconnect frequently (to change query parameters, for example) run the risk of being rate limited.
Twitter does not make public the number of connection attempts which will cause a rate limiting to occur, but there is some tolerance for testing and development. A few dozen connection attempts from time to time will not trigger a limit. However, it is essential to stop further connection attempts for a few minutes if a HTTP 420 response is received. If your client is rate limited frequently, it is possible that your IP will be blocked from accessing Twitter for an indeterminate period of time.
见https://developer.twitter.com/en/docs/tweets/filter-realtime/guides/connecting。