由于twitterapi,我在使用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理解从他停止的那一点开始。有什么想法吗? 谢谢!!!你知道吗
无需使用
islice()
函数,您可以直接对关注者列表进行切片,并使用一个简单的计数器跟踪您在列表中的位置在每个循环之后,只需向计数器添加200,它将从上次完成切片的位置对列表进行切片。你知道吗
您可以使用yield关键字来处理您的列表:
然后您可以按如下方式使用它:
相关问题 更多 >
编程相关推荐