AttributeError: module ‘urllib’ has no attribute ‘urlopen’
import urllib
response = urllib.urlopen('http://www.baidu.com')
print(response.read())
问题描述:使用PYTHON写一个访问任意URL的方法,运行报错: AttributeError: module ‘urllib’ has no attribute ‘urlopen’
解决方案:
由于使用PYTHON2.7版本不支持urllib第三方库。
直接把urllib改为:urllib.request即可,如下代码:
import urllib.request
response = urllib.request.urlopen('http://www.baidu.com')
print(response.read())
本文介绍了一个常见的Python编程问题,当尝试使用urllib库访问URL时遇到AttributeError。问题出现在使用Python 2.7版本时,urllib库中没有urlopen属性。文章提供了简单有效的解决方案,只需将urllib替换为urllib.request。
3万+

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



