我正在尝试从我关注的 Twitter 中提取 10 个受欢迎的(已验证)用户,但每次我收到错误时,错误都是 RuntimeError:生成器引发了 StopIteration。我是python新手,不知道代码哪里错了,所以我发布了整个代码。
def limitt(pointerss, list_name):
while True:
try:
yield pointerss.next()
except tweepy.RateLimitError:
print("\nData points in list = {}".format(len(list_name)))
print('Hit Twitter API rate limit.')
for i in range(3, 0, -1):
print("Wait for {} mins.".format(i * 5))
time.sleep(5 * 60)
except tweepy.error.TweepError:
print('\nCaught TweepError exception')
def extracting_popular_user():
following_list = []
cursor = tweepy.Cursor(api.friends, count=10).pages()
for i, page in enumerate(limitt(cursor, following_list)):
following_list += page
# print(type(following_list))
following_list = [x._json for x in following_list]
# print("hello")
print(len(following_list))
for i in range(0, len(following_list)):
screen_name = following_list[i]['screen_name']
followers_count = following_list[i]['followers_count']
statuses_count = following_list[i]['statuses_count']
verified = following_list[i]['verified']
if verified == True and followers_count > 10000 and statuses_count > 5000:
with open("file.txt", "a") as f:
f.write("%s \n" % (screen_name))
with open("file.txt", "r") as f1:
accounts = f1.readlines()
if len(accounts) == 10:
break
if __name__ == "__main__":
auth = tweepy.OAuthHandler(" ", " ") # enter twitter key
auth.set_access_token(" ", "") # enter twitter access token
api = tweepy.API(auth)
extracting_popular_user()```
我收到以下错误 RuntimeError:生成器引发了 StopIteration。我已经尝试了多次,但仍然没有得到结果。所以需要帮助。
---------------------------------------------------------------------------
StopIteration Traceback (most recent call last)
<ipython-input-10-6e4a410414db> in limitt(pointerss, list_name)
13 try:
---> 14 yield pointerss.next()
15
2 frames
StopIteration:
The above exception was the direct cause of the following exception:
RuntimeError Traceback (most recent call last)
<ipython-input-10-6e4a410414db> in extracting_popular_user()
29 following_list = []
30 cursor = tweepy.Cursor(api.friends, count=10).pages()
---> 31 for i, page in enumerate(limitt(cursor, following_list)):
32 following_list += page
33 # print(type(following_list))
RuntimeError: generator raised StopIteration