Python3.X出现AttributeError: module 'urllib' has no attribute 'urlopen'错误
2017-03-29 09:13
696 查看
研究用Python写爬虫,下载一个网页。报错代码如下
运行时报错:AttributeError: module 'urllib' has no attribute 'urlopen'
在网上查了一些资料,有人说是因为你的这个工程目录下可能有一个自己定义的文件与urllib重名,导致上述代码在引用时实际引用的是自定义的那个urllib,结果查找自己的项目文件夹下也没有重名的文件。其实真正原因是在Python3.X中应该用urllib.request。更改后就不会再出现这个错误了。
运行截图如下:
import urllib def getHtml(url): page = urllib.urlopen(url) html = page.read() return html html = getHtml("http://www.baidu.com") print(html)
运行时报错:AttributeError: module 'urllib' has no attribute 'urlopen'
在网上查了一些资料,有人说是因为你的这个工程目录下可能有一个自己定义的文件与urllib重名,导致上述代码在引用时实际引用的是自定义的那个urllib,结果查找自己的项目文件夹下也没有重名的文件。其实真正原因是在Python3.X中应该用urllib.request。更改后就不会再出现这个错误了。
import urllib.request #print(urllib.__file__) def getHtml(url): page = urllib.request.urlopen(url) html = page.read() return html html = getHtml("http://www.baidu.com") print(html)
运行截图如下:
相关文章推荐
- Python 3.x中导入urllib出现AttributeError: module 'urllib' has no attribute 'urlopen'
- Python3.x中出现 "AttributeError: module 'urllib' has no attribute 'urlopen'" 的解决方法
- python3.6 出现AttributeError: module 'urllib' has no attribute 'urlopen'
- Python2.7 安装mock出现AttributeError: 'module' object has no attribute的解决方法
- Python 2.7.3 AttributeError: 'module' object has no attribute 'urlopen'
- AttributeError: module 'urllib' has no attribute 'urlopen'
- web.py 启动时候出现AttributeError: 'module' object has no attribute 'inet_pton'错误
- web.py 启动时候出现AttributeError: 'module' object has no attribute 'inet_pton'错误
- AttributeError: module 'requests' has no attribute 'get'”,python中”requests模块“get”属性缺失“的错误该如何解决?
- python错误 module 'urllib' has no attribute 'request'
- python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法
- Python报错——AttributeError: module 'PyQt5.QtGui' has no attribute 'QApplication'
- Python基础学习-'module' object has no attribute 'urlopen'解决方法
- python 3.x 错误 ‘generator’ object has no attribute ‘next’
- python3 报错: AttributeError: 'module' object has no attribute 'urlopen'
- 使用Buildozer部署时,出现AttributeError: 'NoneType' object has no attribute 'group'错误的解决
- 'module' object has no attribute 在python开发中出现的原因
- python AttributeError: 'module' object has no attribute 'setdefaultencoding'
- python 3.x 错误 ‘generator’ object has no attribute ‘next’
- 学习Python csv模块遇到AttributeError: module 'csv' has no attribute 'writer'和写入后出现空格问题