python-3.x - 如何改进我的错误处理,以便正确处理 Tweepy 中的异常 StopIteration 并且可以继续执行?

标签 python-3.x error-handling tweepy stopiteration

我有以下函数来获取 Twitter 关注者并将它们写入 MySQL 数据库。我的问题是我的错误处理不能很好地处理 StopIteration 情况。
当我执行代码时,它确实根据 API 限制将详细信息写入数据库,但最后它会生成下面的错误,因此不会执行进一步的代码。
如何改进我的错误处理以便正确处理异常?

StopIteration: The above exception was the direct cause of the following exception: RuntimeError

def limit_handled(cursor):
    while True:
        try:
            yield cursor.next()
        except tweepy.RateLimitError:
            time.sleep(15 * 60)
def writeFollowersToDB(TwitterAPI,DBConnection,SocialHandle ="Microsoft",DatabaseTable="twitter"):
    AboutMe = TwitterAPI.get_user(SocialHandle)
    #print(AboutMe)
    DBCursor=mydb.cursor()
    #Create the SQL INSERT
    SQLInsert="INSERT INTO "+ DatabaseTable + " (SourceHandle,SourceHandleFollowersCount,SourceHandleFollowingCount, Action,DestinationHandle,DestinationHandleFollowersCount,DestinationPublishedLocation,DestinationWeb,CrawlDate) VALUES (%s, %s, %s,%s,%s,%s,%s,%s,%s) ;"
    print(SQLInsert)
    for follows in limit_handled(tweepy.Cursor(TwitterAPI.followers,id=SocialHandle).items()):
        today = date.today()
        try:
            if not follows.url:
                expandedURL =""
            else:
                #print(follows.url)
                expandedURL = follows.entities["url"]["urls"][0]["expanded_url"]
            #print(follows.screen_name, AboutMe.followers_count,AboutMe.friends_count,"from ", follows.location,"with ", " followers "," and provided this expanded URL: ",expandedURL )
            CrawlDate = today.strftime("%Y-%m-%d")
            #Insert into table
            SQLValues =(AboutMe.screen_name,AboutMe.followers_count,AboutMe.friends_count,"isFollowedBy",follows.screen_name,follows.followers_count,follows.location,expandedURL,CrawlDate)
            DBCursor.execute(SQLInsert,SQLValues)
            DBConnection.commit()
            print(AboutMe.screen_name,follows.screen_name,follows.followers_count)
        except StopIteration:
            DBConnection.close()
            break
        except:
            print(e.reason)
            DBConnection.close()
            break



---------------------------------------------------------------------------
StopIteration Traceback (most recent call last)
<ipython-input-2-d095a0b00b72> in limit_handled(cursor)
      3         try:
----> 4             yield cursor.next()
      5         except tweepy.RateLimitError:

C:\Path\site-packages\tweepy\cursor.py in next(self)
    194             # Reached end of current page, get the next page...
--> 195             self.current_page = self.page_iterator.next()
    196             self.page_index = -1

C:\Path\site-packages\tweepy\cursor.py in next(self)
     69         if self.next_cursor == 0 or (self.limit and self.num_tweets == self.limit):
---> 70             raise StopIteration
     71         data, cursors = self.method(cursor=self.next_cursor,



最佳答案

问题是 limit_handling 函数没有 try catch 它抛出的 StopIteration 错误。下面改进的功能对我有用。感谢 @Booboo为了他们的帮助

def limit_handled(cursor):
    while True:
        try:
            yield cursor.next()
        except StopIteration:
            return
        except tweepy.RateLimitError:
            time.sleep(15 * 60)

关于python-3.x - 如何改进我的错误处理,以便正确处理 Tweepy 中的异常 StopIteration 并且可以继续执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62738718/

相关文章:

python - 从可迭代的点生成路径

python-3.x - urllib.error.URLError : <urlopen error unknown url type: 'https>

python - 无法使用网络摄像头读取帧。错误-2147483638

json - Rest API中成功和错误响应的详细信息

java - Spring集成错误消息路由是如何工作的?

python - 将 numpy 数组保存到 csv 会产生不匹配错误

python - 如何使用 python 在乌尔都语文本中应用正则表达式

python - 使用 conda 和 python3k 构建包

node.js - 如何使用异步等待处理同步错误?

python - Writing multiple JSON to CSV in Python - 字典到 CSV