Import error: No module name urllib2

后端 未结 9 942
隐瞒了意图╮
隐瞒了意图╮ 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:33

    NOTE: urllib2 is no longer available in Python 3

    You can try following code.

    import urllib.request 
    res = urllib.request.urlopen('url')
    output = res.read()
    print(output)
    

    You can get more idea about urllib.request from this link.

    Using :urllib3

    import urllib3
    http = urllib3.PoolManager()
    r = http.request('GET', 'url')
    print(r.status)
    print( r.headers)
    print(r.data)
    

    Also if you want more details about urllib3. follow this link.

    0 讨论(0)
  • 2020-11-22 07:35

    That worked for me in python3:

    import urllib.request
    htmlfile = urllib.request.urlopen("http://google.com")
    htmltext = htmlfile.read()
    print(htmltext)
    
    0 讨论(0)
  • The above didn't work for me in 3.3. Try this instead (YMMV, etc)

    import urllib.request
    url = "http://www.google.com/"
    request = urllib.request.Request(url)
    response = urllib.request.urlopen(request)
    print (response.read().decode('utf-8'))
    
    0 讨论(0)
提交回复
热议问题