我对使用游标类的tweepy和分页相当新。我一直在尝试使用游标类来获取特定Twitter用户的所有关注者,但我不断收到错误信息"tweepy.error.TweepError: This method does not perform pagination"
因此,我真的很感激任何帮助,如果有人可以帮助我实现这个任务,使用tweepy获取特定Twitter用户的所有追随者的分页。我到目前为止的代码如下:
import tweepy
consumer_key='xyz'
consumer_secret='xyz'
access_token='abc'
access_token_secret='def'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
user = api.get_user('somehandle')
print user.name
followers = tweepy.Cursor(user.followers)
temp=[]
for user in followers.items():
temp.append(user)
print temp
#the following part works fine but that is without pagination so I will be able to retrieve at #most 100 followers
aDict = user.followers()
for friend in aDict:
friendDict = friend.__getstate__()
print friendDict['screen_name']
答案 0 :(得分:6)
有一种名为followers_ids
的方便方法。对于给定的screen_name
(或id
,user_id
或cursor
),它最多可返回5000个关注者{twitter api limit)ID。
然后,您可以在python中手动分页这些结果,并为每个块调用lookup_users
。只要lookup_users
一次只能处理100个用户ID(twitter api limit),将块大小设置为100就是合乎逻辑的。
这是代码(分页部分来自here):
import itertools
import tweepy
def paginate(iterable, page_size):
while True:
i1, i2 = itertools.tee(iterable)
iterable, page = (itertools.islice(i1, page_size, None),
list(itertools.islice(i2, page_size)))
if len(page) == 0:
break
yield page
auth = tweepy.OAuthHandler(<consumer_key>, <consumer_secret>)
auth.set_access_token(<key>, <secret>)
api = tweepy.API(auth)
followers = api.followers_ids(screen_name='gvanrossum')
for page in paginate(followers, 100):
results = api.lookup_users(user_ids=page)
for result in results:
print result.screen_name
希望有所帮助。