Import error: No module name urllib2

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

    As stated in the urllib2 documentation:

    The urllib2 module has been split across several modules in Python 3 named urllib.request and urllib.error. The 2to3 tool will automatically adapt imports when converting your sources to Python 3.

    So you should instead be saying

    from urllib.request import urlopen
    html = urlopen("http://www.google.com/").read()
    print(html)
    

    Your current, now-edited code sample is incorrect because you are saying urllib.urlopen("http://www.google.com/") instead of just urlopen("http://www.google.com/").

提交回复
热议问题