gpt4 book ai didi

python - Python Twitter Tools-处理Twitter 404和401 API错误

转载 作者:行者123 更新时间:2023-12-03 08:54:07 25 4
gpt4 key购买 nike

查询Twitter API时出现404和401错误,由于无法处理异常,我拉 friend 的脚本中断了。
研究并尝试添加以下try/except条款,但未记录异常,并且for循环停止。

first = 0
last = 15
while last < len(list)+16: #while last group of 15 items is lower than number of items in list#
for item in list[first:last]: #parses twitter IDs in the list by groups of 15#
try:
results = twitter.friends.ids(skip_status="true",include_user_entities="false",count ="5000",user_id=item) #API query#
results = str(results)
text_file = open("output.txt", "a") #creates empty or opens current txt output / change path to desired output#
text_file.write(str(item) + "," + results + "\n") #adds twitter ID, resulting friends list, and a line skip to the txt output#
text_file.close()
break
except ValueError:
text_file = open("output.txt", "a") #creates empty or opens current txt output / change path to desired output#
text_file.write(str(item) + "," + "ERROR" + "\n") #adds twitter ID, resulting friends list, and a line skip to the txt output#
text_file.close()
print "Succesfully processed users " + str(list[first:last]) #returns recently processed group of 15 users#
first = first + 15 #updates list navigation to move on to next group of 15#
last = last + 15
time.sleep(1000) #suspends activities for 1000 seconds to respect rate limit#

还研究了基于http响应 header 构建if语句,但是我不明白如何插入Python Twitter Tools“response.headers.get('h')”来做到这一点。处理和记录这些异常并使脚本继续提取数据的最佳方法是什么?

最佳答案

删除break块末尾的try:,并在continue块末尾添加except:。即:

first = 0
last = 15
while last < len(list)+16: #while last group of 15 items is lower than number of items in list#
for item in list[first:last]: #parses twitter IDs in the list by groups of 15#
try:
results = twitter.friends.ids(skip_status="true",include_user_entities="false",count ="5000",user_id=item) #API query#
results = str(results)
text_file = open("output.txt", "a") #creates empty or opens current txt output / change path to desired output#
text_file.write(str(item) + "," + results + "\n") #adds twitter ID, resulting friends list, and a line skip to the txt output#
text_file.close()
# break is not needed here!
except ValueError:
text_file = open("output.txt", "a") #creates empty or opens current txt output / change path to desired output#
text_file.write(str(item) + "," + "ERROR" + "\n") #adds twitter ID, resulting friends list, and a line skip to the txt output#
text_file.close()
continue # You have dealt with the exception so don't stop
print "Succesfully processed users " + str(list[first:last]) #returns recently processed group of 15 users#
first = first + 15 #updates list navigation to move on to next group of 15#
last = last + 15
time.sleep(1000) #suspends activities for 1000 seconds to respect rate limit#

从评论转移到保留。

关于python - Python Twitter Tools-处理Twitter 404和401 API错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31783993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com