How to continue other link when Urllib 404 error

Question:

I’m trying to download images from a csv with lot of links. Works fine until some link is broken (urllib.error.HTTPError: HTTP Error 404: Not Found) .

import pandas as pd
import urllib.request
import urllib.error
opener = urllib.request.build_opener()

def url_to_jpg (i,url,file_path) :

        filename="image-{}".format(i)
        full_path = "{}{}".format(file_path, filename)
        opener.addheaders = [('User-Agent', 'Chrome/5.0')]
        urllib.request.install_opener(opener)
        urllib.request.urlretrieve(url,full_path)
        print ("{} Saved".format(filename))
        return None

filename="listado.csv"

file_path="/Users/marcelomorelli/Downloads/tapas/imagenes"

urls=pd.read_csv(filename)

for i, url in enumerate(urls.values):
    url_to_jpg (i,url[0],file_path)

Thanks!

Any idea how can I made to Python continue to the other link in the list everytime gets that error?

Asked By: marcos9718

||

Answers:

You can use a try pattern and ignore errors.
Your code would look like this:

for i, url in enumerate(urls.values):
    try:
        url_to_jpg(i,url[0],file_path)
    except Exception as e:
        print(f"Failed due to: {e}")

Reference: https://docs.python.org/3/tutorial/errors.html

Answered By: cavalcantelucas
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.