-1

Running python 3 with this code, I cannot for the life of me catch this 404 error. If an image is not found, urllib.error.HTTPError: HTTP Error 404: Not Found is raised at 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]
Micks Ketches
  • 456
  • 1
  • 4
  • 14

1 Answers1

1

The error is raised at

dl_image = urllib.request.urlretrieve(cam_url, "thumbnail" + str(1 + img_num[0]) + ".jpg") 

Simply place the try statement before that line

JRodDynamite
  • 12,325
  • 5
  • 43
  • 63