ValueError: unknown url type in urllib2, though the url is fine if opened in a browser

后端 未结 4 1948
灰色年华
灰色年华 2020-12-15 05:55

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.         


        
相关标签:
4条回答
  • 2020-12-15 06:09

    You can use the urlparse function for that I think :

    Python User Documentation

    0 讨论(0)
  • 2020-12-15 06:16

    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'
        ..: if not urlparse(url).scheme:
        ..:     url = 'http://' + url
        ..: 
        ..: url
    Out[1]: 'http://www.myurl.com'
    
    0 讨论(0)
  • 2020-12-15 06:20

    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://.

    0 讨论(0)
  • 2020-12-15 06:23

    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/.

    0 讨论(0)
提交回复
热议问题