Chillee / coursera-dl-all

MIT License
190 stars 54 forks source link

error module doesn't exist for urllib in Python 2.x #14

Closed joshuawn closed 8 years ago

joshuawn commented 8 years ago

The error module is a new addition meant for Python 3. An equivalent for urllib.error.HTTPError in Python 2.x would be to use urllib2.HTTPError A solution would be to check the version of Python used to run the script (sys.version) and write conditionals to determine imports and modules. An example can be found here: http://stackoverflow.com/questions/1875259/importing-a-module-based-on-installed-python-version

You may also want to handle multiple errors at once, since urllib.urlretrieve can also produce an OSError if a connection cannot be established.

For instance,

except (OSError, urllib.error.HTTPError, urllib.error.URLError) as e:
         print("Failed to download "+url)
         continue
Chillee commented 8 years ago

I think I'm planning on switching over to requests, as that appears to be the more "modern" way to do things.

Chillee commented 8 years ago

I have changed from urllib to requests as requests seems to be best practice.

Also it seems to have worked better for one of the guys raising an issue.