由于使用了twitter API,我已经遇到了使用python检索追随者信息的问题。如您所知,API会在某个超时或速率限制达到后降低。我的想法是减少我必须抓取的追随者列表,例如200个屏幕名称,请等待,然后继续。为此,我使用了islice:
while True:
lines =[x.rstrip('\n') for x in islice(followers, 200)]
for i in lines:
try:
# Request general user information
resp = twitter.show_user(screen_name=i)
print('Retrieving information for'+ ' '+str(i))
spinner1.start()# Append fields to list
user_info.append([resp['id'],
resp['screen_name'],
resp['name'],
resp['lang'],
resp['location'],
resp['created_at'],
resp['description'],
resp['followers_count'],
resp['friends_count'],
resp['statuses_count'],
resp['favourites_count'],
resp['listed_count']])
spinner1.stop()
time.sleep(1)
except:
print('>>>>>' + 'This User: ' + str(x) + ' Is not accessible' + '<<<<<')
time.sleep(6301)
if not lines:
break
问题是循环从列表的开头再次开始。我没有使python理解从他停止的那一点开始。有任何想法吗? 非常感谢!!!
答案 0 :(得分:0)
不需要islice()
函数,您可以直接对关注者列表进行切片,并使用简单的计数器来跟踪您在列表中的位置;
cnt = 0
while True:
lines =[x.rstrip('\n') for x in followers[cnt:cnt+200])]
for i in lines:
try:
# Request general user information
resp = twitter.show_user(screen_name=i)
print('Retrieving information for'+ ' '+str(i))
spinner1.start()# Append fields to list
user_info.append([resp['id'],
resp['screen_name'],
resp['name'],
resp['lang'],
resp['location'],
resp['created_at'],
resp['description'],
resp['followers_count'],
resp['friends_count'],
resp['statuses_count'],
resp['favourites_count'],
resp['listed_count']])
spinner1.stop()
time.sleep(1)
except:
print('>>>>>' + 'This User: ' + str(x) + ' Is not accessible' + '<<<<<')
time.sleep(6301)
cnt += 200
if not lines:
break
每个循环之后,您只需将200加到计数器,它将从您上一次完成切片的位置开始对列表进行切片。
答案 1 :(得分:-2)
您可以使用yield关键字来处理列表:
def process_followers():
for i in range(0, len(followers), 200):
lines = followers[i:i + 200]
for i in lines:
try:
# Request general user information
resp = twitter.show_user(screen_name=i)
print('Retrieving information for' + ' ' + str(i))
spinner1.start() # Append fields to list
user_info.append([resp['id'],
resp['screen_name'],
resp['name'],
resp['lang'],
resp['location'],
resp['created_at'],
resp['description'],
resp['followers_count'],
resp['friends_count'],
resp['statuses_count'],
resp['favourites_count'],
resp['listed_count']])
spinner1.stop()
time.sleep(1)
except:
print('>>>>>' + 'This User: ' + str(x) + ' Is not accessible' + '<<<<<')
yield
然后您可以按以下方式使用它:
processor = process_followers()
try:
while True:
processor.__next__()
time.sleep(6301)
except StopIteration:
pass