Python Twitter工具 - 处理twitter 404和401 API错误

时间:2015-08-03 09:35:53

标签: python api twitter error-handling exception-handling

我在查询Twitter API时遇到404和401错误,我的脚本因为没有处理异常而拉断了朋友。 已经研究并尝试添加以下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响应头的if语句,但我不明白如何插入Python Twitter Tools“response.headers.get('h')”这样做。处理和记录这些异常并使脚本继续提取数据的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

删除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#

从评论转移到保存。