gpt4 book ai didi

for-loop - tweepy 中的循环错误

转载 作者:行者123 更新时间:2023-12-04 06:37:40 26 4
gpt4 key购买 nike

我最近一直在玩 tweepy,我试图拉动给定用户的关注者和关注者。

 followingids = []
followids = []
userid = "Someone"#sets target

for page in tweepy.Cursor(api.followers_ids, screen_name=userid).pages():#gets the followers for userID
followingids.extend(page)
time.sleep(60)#keeps us cool with twitter


for page in tweepy.Cursor(api.friends_ids, screen_name=userid).pages():#gets the followers for userID
followids.extend(page)
time.sleep(60)#keeps us cool with twitter

#where weirdness starts
print len(followingids), "followers have been gathered from", userid
print len(followids), " users are followed by ", userid

followingusers = api.lookup_users(user_ids=followingids)#ieterates through the list of users and prints them
followedusers = api.lookup_users(user_ids=followids) #<does not work but above does


print "users of following", userid
for u in followingusers:
print u.screen_name


print "users followed by", userid
for s in followedusers:
print s.screen_name

第二个 print for 循环给出了这个错误:

 Traceback (most recent call last):
File "twitterBot.py", line 30, in <module>
followedusers = api.lookup_users(user_ids=followids) #<does not work but above does
File "/usr/local/lib/python2.7/dist-packages/tweepy/api.py", line 160, in lookup_users
return self._lookup_users(list_to_csv(user_ids), list_to_csv(screen_names))
File "/usr/local/lib/python2.7/dist-packages/tweepy/binder.py", line 230, in _call
return method.execute()
File "/usr/local/lib/python2.7/dist-packages/tweepy/binder.py", line 203, in execute
raise TweepError(error_msg, resp)
tweepy.error.TweepError: [{u'message': u'Too many terms specified in query.', u'code': 18}]

followedusers 和 followingusers 都有 twitter id 号码,我直接剪切并粘贴了用于打印的代码,那么为什么第一个有效而第二个无效?

最佳答案

您的代码未损坏,不一定。推特limits you to returning 100 users使用 GET 用户查找(这是 api.lookup_users 所做的)。请求超过 100 将导致 Tweepy 错误代码 18,您仅针对 followed users 查询获得该错误代码,这看起来像您查询的用户 followed more than 100 users 但 followed by less than 100.

一个简单的解决方案是迭代搜索关注者/关注用户的列表(如果您希望代码更健壮,这两者都是安全的)。每 100 个用户标识进行一次迭代,并使用 api.lookup_users 一次最多查找 100 个。

关于for-loop - tweepy 中的循环错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24131562/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com