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