用这段代码运行python 3,我不能为我的生活抓住这404错误。如果找不到图像,则会引发urllib.error.HTTPError: HTTP Error 404: Not Found
dl_image = urllib.request.urlretrieve(cam_url, "thumbnail" + str(1 + img_num[0]) + ".jpg")
:
import requests
# import json
# import re
import random
import urllib
def random_cam(img_num=[0]):
cam_url = url + camera_IDs[img_num[0]] + trailing_url
dl_image = urllib.request.urlretrieve(cam_url, "thumbnail" + str(1 + img_num[0]) + ".jpg")
if img_num == [0]: # makes sure to only shuffle camera order once to avoid duplicate camera thumbnails
random.shuffle(camera_rand)
try:
dl_image
except urllib.error.HTTPError:
print ("image not found or camera is offline")
img_num[0] += 2 # skip over index that throws an error and download next image in line
dl_image
else:
print (cam_url)
img_num[0] += 1
return img_num[0]
答案 0 :(得分:1)
错误在
处引发dl_image = urllib.request.urlretrieve(cam_url, "thumbnail" + str(1 + img_num[0]) + ".jpg")
只需将try
语句放在该行