Import error: No module name urllib2

后端 未结 9 974
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 06:42

Here\'s my code:

import urllib2.request

response = urllib2.urlopen(\"http://www.google.com\")
html = response.read()
print(html)

Any help?

9条回答
  •  忘了有多久
    2020-11-22 07:31

    Some tab completions to show the contents of the packages in Python 2 vs Python 3.

    In Python 2:

    In [1]: import urllib
    
    In [2]: urllib.
    urllib.ContentTooShortError      urllib.ftpwrapper                urllib.socket                    urllib.test1
    urllib.FancyURLopener            urllib.getproxies                urllib.splitattr                 urllib.thishost
    urllib.MAXFTPCACHE               urllib.getproxies_environment    urllib.splithost                 urllib.time
    urllib.URLopener                 urllib.i                         urllib.splitnport                urllib.toBytes
    urllib.addbase                   urllib.localhost                 urllib.splitpasswd               urllib.unquote
    urllib.addclosehook              urllib.noheaders                 urllib.splitport                 urllib.unquote_plus
    urllib.addinfo                   urllib.os                        urllib.splitquery                urllib.unwrap
    urllib.addinfourl                urllib.pathname2url              urllib.splittag                  urllib.url2pathname
    urllib.always_safe               urllib.proxy_bypass              urllib.splittype                 urllib.urlcleanup
    urllib.base64                    urllib.proxy_bypass_environment  urllib.splituser                 urllib.urlencode
    urllib.basejoin                  urllib.quote                     urllib.splitvalue                urllib.urlopen
    urllib.c                         urllib.quote_plus                urllib.ssl                       urllib.urlretrieve
    urllib.ftpcache                  urllib.re                        urllib.string                    
    urllib.ftperrors                 urllib.reporthook                urllib.sys  
    

    In Python 3:

    In [2]: import urllib.
    urllib.error        urllib.parse        urllib.request      urllib.response     urllib.robotparser
    
    In [2]: import urllib.error.
    urllib.error.ContentTooShortError  urllib.error.HTTPError             urllib.error.URLError
    
    In [2]: import urllib.parse.
    urllib.parse.parse_qs          urllib.parse.quote_plus        urllib.parse.urldefrag         urllib.parse.urlsplit
    urllib.parse.parse_qsl         urllib.parse.unquote           urllib.parse.urlencode         urllib.parse.urlunparse
    urllib.parse.quote             urllib.parse.unquote_plus      urllib.parse.urljoin           urllib.parse.urlunsplit
    urllib.parse.quote_from_bytes  urllib.parse.unquote_to_bytes  urllib.parse.urlparse
    
    In [2]: import urllib.request.
    urllib.request.AbstractBasicAuthHandler         urllib.request.HTTPSHandler
    urllib.request.AbstractDigestAuthHandler        urllib.request.OpenerDirector
    urllib.request.BaseHandler                      urllib.request.ProxyBasicAuthHandler
    urllib.request.CacheFTPHandler                  urllib.request.ProxyDigestAuthHandler
    urllib.request.DataHandler                      urllib.request.ProxyHandler
    urllib.request.FTPHandler                       urllib.request.Request
    urllib.request.FancyURLopener                   urllib.request.URLopener
    urllib.request.FileHandler                      urllib.request.UnknownHandler
    urllib.request.HTTPBasicAuthHandler             urllib.request.build_opener
    urllib.request.HTTPCookieProcessor              urllib.request.getproxies
    urllib.request.HTTPDefaultErrorHandler          urllib.request.install_opener
    urllib.request.HTTPDigestAuthHandler            urllib.request.pathname2url
    urllib.request.HTTPErrorProcessor               urllib.request.url2pathname
    urllib.request.HTTPHandler                      urllib.request.urlcleanup
    urllib.request.HTTPPasswordMgr                  urllib.request.urlopen
    urllib.request.HTTPPasswordMgrWithDefaultRealm  urllib.request.urlretrieve
    urllib.request.HTTPRedirectHandler     
    
    
    In [2]: import urllib.response.
    urllib.response.addbase       urllib.response.addclosehook  urllib.response.addinfo       urllib.response.addinfourl
    

提交回复
热议问题