简体   繁体   中英

DeadLink exception from Python2 to Python3

I found this code written in Python 2.7 to bypass a deadlink while reading a list of urls and retrieving their content:

for i in xrange(lines):
    try:
        t = urllib2.urlopen(urllib2.Request(lines[i]))
        deadlinkfound = False
    except:
        deadlinkfound = True
    if not(deadlinkfound):
        urllib.urlretrieve(lines[i], "Images/imag" + "-%s" % i)

It worked fine in Python2 but I can't find the equivalent in Python3 because of the urllib2 merging.

You can do the exact same thing with urllib.request here. Don't catch every conceivable exception, only catch what is reasonably going to be thrown:

from urllib import request, error
from http.client import HTTPException

for i, url in enumerate(lines):
    try:
        t = request.urlopen(request.Request(url, method='HEAD'))
    except (HTTPException, error.HTTPError):
        continue
    request.urlretrieve(url, 'Images/imag-{}'.format(i))

This code does the same, but more efficiently.

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