Reputation: 735
I have been toying with tweepy recently, I am trying to pull the followers and following of a given user.
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
The second print for loop gives this error:
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}]
both followedusers and followingusers have twitter id numbers, I literally cut and pasted the code for printing so why does the first one work and the second one not work?
Upvotes: 2
Views: 1057
Reputation: 4129
Your code isn't broken, necessarily. Twitter limits you to returning 100 users with a GET user lookup (which is what api.lookup_users
does). Requesting more than 100 will cause the Tweepy error code 18 which you got only for the followed users query, which looks like the user you queried follows more than 100 users but is followed by less than 100.
A simple solution would be to search iteratively through the list of followers/followed users (both to be safe if you want your code to be more robust). Iterate through every 100 userids and use api.lookup_users
to only look up a maximum of 100 at a time.
Upvotes: 7