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

后端 未结 4 1949
灰色年华
灰色年华 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: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'
    

提交回复
热议问题