Twitter API 404错误 - Python(续)

时间:2015-08-05 13:48:08

标签: python api for-loop twitter error-handling

我正在尝试在python脚本中使用来自twitter API的GET用户/查找来根据用户ID列表识别屏幕名称。该脚本似乎无法处理404错误,因为我假设twitter没有找到具有100个用户ID的整个请求,因此for循环甚至没有开始。如果100个ID之一将导致整个请求达到404,我如何一次迭代100多个用户ID,同时仍然遵守速率限制?有没有办法处理此错误,同时仍然获得同一请求中的其他ID的响应?我对“除了Valueerror”的实验似乎没有解决这个问题......

我非常感谢你能给出的任何建议或提示!

while total_request_counter <= request_total:
  while list_counter_last <= len(list)+100: #while last group of 100 items is lower or equal than total number of items in list#:
    while current_request_counter < 180:
        response = twitter.users.lookup(include_entities="false",user_id=",".join(list[list_counter_first:list_counter_last])) #API query for 100 users#
        for item in list[list_counter_first:list_counter_last]: #parses twitter IDs in the list by groups of 100 to record results#
            try: #necessary for handling errors#
                results = str(response)
                results = results[results.index("u'screen_name': u'",results.index(item)) + 18:results.index("',",results.index("u'screen_name': u'",results.index(item)) + 18)]#looks for username section in the query output#
                text_file = open("output_IDs.txt", "a") #opens current txt output / change path to desired output#
                text_file.write(str(item) + "," + results + "\n") #adds twitter ID, info lookup result, and a line skip to the txt output#
                text_file.close()
                error_counter = error_counter + 1
                print str(item) + " = " + str(results)
            except: #creates exception to handle errors#
                text_file = open("output_IDs.txt", "a") #opens current txt output / change path to desired output#
                text_file.write(str(item) + "," + "ERROR " + str(response.headers.get('h')) + "\n") #adds twitter ID, error code, and a line skip to the txt output#
                text_file.close()
                print str(item) + " = ERROR"
                continue 
        print "Succesfully processed " + str(error_counter) + " users of request number " + str(current_request_counter + 1) + ". " + str(179 - current_request_counter) + " requests left until break."   #confirms latest batch processing#
        print ""
        list_counter_first = list_counter_first + 100 #updates list navigation to move on to next group of 100#
        list_counter_last = list_counter_last + 100
        error_counter = 0 # resets error counter for the next round#
        current_request_counter = current_request_counter + 1 #updates current request counter to stay below rate limit of 180#
t = str(datetime.now())
print ""
print "Taking a break, back in 16 minutes. Approximate time left: " + str((len(list)/18000*15)-(15*total_request_counter)) + " minutes. Current timestamp: " + t
print ""
current_request_counter = 0
total_request_counter = total_request_counter + 1
time.sleep(960) #suspends activities for 16 minutes to respect rate limit#

0 个答案:

没有答案