简体   繁体   中英

Catch the 400 Bad request before stopping the script

I downloaded files like this and it sometimes returns 400 request.

f = urllib.request.urlretrieve(url,"filename")

So I would like to check if it returns 400 error ,skip and continue to the next procedure.

However if it returns 400 , the program stops with this error.

urllib.error.HTTPError: HTTP Error 400: Bad Request

How can I don't let 400 error stop the program?

Use try catch blocks. Assuming that urllib is already imported

try: 
    f = urllib.request.urlretrieve(url,"filename")
except urllib.error.HTTPError as e:
    print("Error: ", e)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM