[英]429 error while fetching Twitter follows for a user using Twython
I have seen a few things on the site trying to help with this issue but I couldn't make head or tail as to what I'm doing wrong. 我在网站上看到了一些可以帮助解决此问题的信息,但对于做错的事情我无能为力。
This bit of code is supposed to get a list of followers but no matter what I try I get a 429 error from the Twitter API: 这段代码应该可以获取关注者列表,但是无论我怎么尝试,我都会从Twitter API中收到429错误:
def get_follow_list():
next_cursor = -1
while next_cursor != 0:
response = twitter.get_followers_list(screen_name=current_user, cursor=next_cursor)
following = response['users']
follow_list = [following]
time.sleep(1)
cursor = response['next_cursor']
return (follow_list)
How would I go about resolving this problem? 我将如何解决这个问题?
Edit: The code from the answer given is great but I get this error while trying to print the value from it: "UnicodeEncodeError: 'UCS-2' codec can't encode characters in position 205571-205571: Non-BMP character not supported in Tk". 编辑:给出的答案中的代码很棒,但是尝试从中打印值时出现此错误:“ UnicodeEncodeError:“ UCS-2”编解码器无法编码位置205571-205571中的字符:不支持非BMP字符在Tk中”。 This in turn causes the problem calling the GUI class as stated earlier. 这进而导致调用GUI类的问题,如前所述。 I'm not sure how to change the encoding of a list to something the listbox in my app can handle. 我不确定如何将列表的编码更改为应用程序中的列表框可以处理的内容。
As described in the Twitter Error Codes and Responses , a 429
response code means Too Many Requests
. 如Twitter错误代码和响应中所述 , 429
响应代码表示Too Many Requests
。 Therefore, the error is not in the code syntax per se, but instead in the number of calls you are performing on the Twitter API. 因此,错误不是代码语法本身,而是您在Twitter API上执行的调用次数。 Please take a look at the REST API Rate Limiting documentation to learn how to track how many calls you can perform (especially using the X-Rate-Limit-Remaining
and other HTTP headers), and the rate limiting details for each REST endpoint . 请查看REST API速率限制文档,以了解如何跟踪可以执行的调用次数(尤其是使用X-Rate-Limit-Remaining
和其他HTTP标头),以及每个REST端点的速率限制详细信息 。
Regarding your question about how to get to paginate after the first 20 results, check using cursors . 关于前20个结果后如何进行分页的问题,请使用光标检查。 Here, the condition on the cursor should be while cursor != 0:
in order to be able to go to the next page. 在这里,光标的条件应该是while cursor != 0:
以便能够转到下一页。 Then, it involves making sure you are not making too many calls to the Twitter API. 然后,它涉及确保您没有对Twitter API进行太多调用。
That being said, here is a better solution for you leveraging GET friends/ids
. 话虽如此,对于您来说,这是一个更好的解决方案,可以利用GET friends/ids
。 This enables you to retrieve the ids of users you follow, 5000 at once (instead of 20), and you can hydrate them just after using GET users/lookup
. 这样一来,您可以一次检索5000个用户ID(而不是20个),然后使用GET users/lookup
它们进行水化处理。 This will work for a large number of followings without the need to pause between each individual calls: 这将适用于大量追随者,而无需在每个单独的调用之间暂停:
def get_follow_list():
users = []
users_ids = []
# Fetch the followings as a cursored collection (up to 5000 per call).
cursor = -1
while cursor != 0:
response = twitter.get_friends_ids(screen_name=current_user, cursor=cursor)
users_ids += response['ids']
cursor = response['next_cursor']
# Lookup the users by chunks of 100.
for i in range(0, len(users_ids), 100):
chunk = users_ids[i:i + 100]
users += twitter.lookup_user(user_id=chunk)
# Return the user objects.
return (users)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.