Skip to content Skip to sidebar Skip to footer

Valueerror: Unknown Url Type In Urllib2, Though The Url Is Fine If Opened In A Browser

Basically, I am trying to download a URL using urllib2 in python. the code is the following: import urllib2 req = urllib2.Request('www.tattoo-cover.co.uk') req.add_header('User-age

Solution 1:

When you enter a URL in a browser without the protocol, it defaults to HTTP. urllib2 won't make that assumption for you; you need to prefix it with http://.

Solution 2:

You have to use a complete URL including the protocol, not just specify a host name.

The correct URL would be http://www.tattoo-cover.co.uk/.

Solution 3:

You can use the method urlparse from urllib (Python 3) to check the presence of an addressing scheme (http, https, ftp) and concatenate the scheme in case it is not present:

In [1]: from urllib.parse import urlparse
    ..: 
    ..: url = 'www.myurl.com'
    ..: ifnoturlparse(url).scheme:
    ..:     url = 'http://' + url
    ..: 
    ..: url
Out[1]: 'http://www.myurl.com'

Solution 4:

You can use the urlparse function for that I think :

Python User Documentation

Post a Comment for "Valueerror: Unknown Url Type In Urllib2, Though The Url Is Fine If Opened In A Browser"