python3中urllib2的导入问题:ImportError: No module named 'urllib2'


#import urllib2, urllib
response = urlopen('http://www.baidu.com/')
html = response.read()
print (html)


运行提示 ImportError: No module named 'urllib2'

因为在py3中

urllib2模块已分为Python 3中的几个模块命名urllib.requesturllib.error

2to3将源转换为Python 3时,工具将自动调整导入。这句话啥意思?

改成 :

from urllib.request import urlopen
response = urlopen('http://www.baidu.com/')
html = response.read()
print (html)

就可以了

参考:https://stackoverflow.com/questions/2792650/import-error-no-module-name-urllib2


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM