使用 twitter api 从 twitter 中提取 10 个流行用户

问题描述

我正在尝试从我关注的 Twitter 中提取 10 个流行(已验证)用户,但每次出现错误时,错误都是 运行时错误生成器引发了 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,-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: generator raise 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,following_list)):
         32         following_list += page
         33     # print(type(following_list))

    RuntimeError: generator raised stopiteration

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)