【发布时间】:2015-07-24 20:42:29
【问题描述】:
我正在编写一个程序来获取给定用户的关注者,然后使用关注者的列表来获取他们的关注者等等。我到目前为止的代码如下所示:
import tweepy
import time
#insert your Twitter keys here
consumer_key ='key'
consumer_secret='secret'
access_token='accesstoken'
access_secret='accesssecret'
auth = tweepy.auth.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
list= open('/home/acrocephalus/GitHub/TweetViz/list.txt','w')
if(api.verify_credentials):
print '-------------------------\n*** You are logged in ***\n-------------------------'
#Set starting Twitter handle
username = ['moixera']
user = tweepy.Cursor(api.followers, screen_name=username).items()
#Set the number of levels to follow
depth=3
#Start extracting each level followers
while depth != 0:
for handle in username:
print '\n\n Getting followers from: @' + handle+'\n\n'
user = tweepy.Cursor(api.followers, screen_name=handle).items()
while True:
try:
u = next(user)
list.write(u.screen_name +'\n')
print u.screen_name
except:
time.sleep(15*60)
print 'We have exceeded the rate limit. Sleeping for 15 minutes'
u = next(user)
list.write(u.screen_name +'\n')
print u.screen_name
username = list.read().splitlines()
print 'Moving to next username'
depth = depth-1
list.close()
问题是它从第一个用户开始,获取她的关注者,但没有继续她的关注者列表。我认为问题出在while 循环中。当它完成获得追随者时,它会跳转到except 部分。期望的行为是,当它完成检索追随者时,它会跳转到for 循环的开头。当程序达到 Twitter 的 API 命中限制并因此超时 15 分钟时,程序应该跳转到 while 循环的 except 部分。任何人都可以帮忙吗?
干杯!
丹妮
【问题讨论】: