1.出现的问题
AttributeError: 'module' object has no attribute 'urlopen'
2.为什么出现这种问题
(1)首先是看一下urllib包里有什么:
突然发现并没有urllib.urlopen
(2)再看看urllib.request里有什么

你会发现urlopen竟然在这里(ΩДΩ)!是真滴调皮ヾ(o・ω・)ノ!
3.究其原因
python3中官方对以前python2中的urllib2,urlparse等的一些包整合到urllib包之中了。
官文是这样写的:
a new urllib package was created. It consists of code from urllib, urllib2, urlparse, and robotparser. The old modules have all been removed. The new package has five submodules: urllib.parse, urllib.request, urllib.response, urllib.error, and urllib.robotparser. The urllib.request.urlopen() function uses the url opener from urllib2. (Note that the unittests have not been renamed for the beta, but they will be renamed in the future.)
所以要使用urlopen的话,你就要from urllib.request import urlopen咯~
解决Python urllib urlopen错误
本文解决了Python3中使用urllib请求网页时遇到的AttributeError错误。原因是Python3将urllib2等模块整合到了新的urllib包中,并对其进行了重新组织。文章详细解释了如何正确导入urlopen函数。
3万+

被折叠的 条评论
为什么被折叠?



