Hyungsoo Kim
Hyungsoo Kim

Reputation: 39

is it possible to catch 404 error without exit run in python?

I want to download a bunch of url list by:

urllib.request.urlretrieve(url, filename)

but sadly, few links are broken. and when urlretrieve meets that broken link

Traceback (most recent call last):
File "D:/Users/hyungsoo/PycharmProjects/untitled/check.py", line 71, in <module>
....blah_blah....
urllib.error.HTTPError: HTTP Error 404: Not Found

give me this error sign and program exited.

how to pass the broken url?

and moreover, is it possible to program tells me what link is broken?

Upvotes: 0

Views: 288

Answers (1)

louie mcconnell
louie mcconnell

Reputation: 761

You could try a "try...except..." statement, which you may find helpful. The "try" portion will attempt the given code, but the "except" portion will be ready for the error message. It is really a wonderful feature. See teh following example:

while True:
...     try:
...         x = int(raw_input("Please enter a number: "))
...         break
...     except ValueError:
...         print "Oops!  That was no valid number.  Try again..."

Note: Code taken from python APIs.

Upvotes: 1

Related Questions