Python 429为使用Twython的用户获取Twitter时出错
我在网站上看到了一些试图帮助解决这个问题的东西,但我不知道我做错了什么 这段代码应该会得到一个追随者列表,但不管我怎么做,我都会从Twitter API中得到一个429错误:Python 429为使用Twython的用户获取Twitter时出错,python,twitter,twython,Python,Twitter,Twython,我在网站上看到了一些试图帮助解决这个问题的东西,但我不知道我做错了什么 这段代码应该会得到一个追随者列表,但不管我怎么做,我都会从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_curso
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)
我将如何着手解决这个问题
编辑:给出的答案中的代码很好,但我在尝试打印其中的值时遇到了此错误:“UnicodeEncodeError:'UCS-2'编解码器无法对205571-205571位置的字符进行编码:Tk中不支持非BMP字符”。这反过来会导致调用GUI类时出现问题,如前所述。我不确定如何将列表的编码更改为我的应用程序中的列表框可以处理的内容。如中所述,429
响应代码意味着请求太多。因此,错误不在于代码语法本身,而在于您在twitterapi上执行的调用数量。请查看文档,了解如何跟踪您可以执行的呼叫数(特别是使用X-Rate-Limit-Remaining
和其他HTTP头),以及
关于您关于如何在前20个结果之后进行分页的问题,请检查。此处,光标上的条件应为while cursor!=0:
以便能够转到下一页。然后,它涉及到确保您没有对twitterapi进行过多的调用
也就是说,这里有一个更好的解决方案。这使您能够检索您跟踪的用户的ID,一次检索5000个(而不是20个),并且您可以在使用后立即将其水合。这将适用于大量以下情况,而无需在每次通话之间暂停:
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)
我知道这一点,但这并不能帮助我找到下一个/最后20个我关注的人。@bricky149我更新了上面的回复,说明了分页不起作用的原因,还有一个更高效的代码示例,它使用不同的API端点来收集你关注的用户。谢谢你的清理,但我现在遇到了另一个问题。这些代码现在导致一个错误:“\U tkinter.TclError:字符U+1f44d超出了Tcl允许的范围(U+0000-U+FFFF)”,这是由调用GUI类引起的。