Python 3.x中导入urllib出现AttributeError: module 'urllib' has no attribute 'urlopen'
来源:互联网 发布:tc编程怎么收费 编辑:程序博客网 时间:2024/06/05 08:22
今天在研究Python爬虫机制,起初遇到一些坑,把我自己的经历写出来,分享大家,少踩坑
开始代码入错(错误代码),不过个人建议有些坑踩一踩还是有必要的,加深记忆
import urllibdef run_demo(): f=urllib.urlopen('http://www.baidu.com') print(f.readline())if __name__=='__main__': run_demo()
执行结果如下
Traceback (most recent call last):
File "D:/gamepython/pabug/__init__.py", line 9, in <module>
run_demo()
File "D:/gamepython/pabug/__init__.py", line 5, in run_demo
f=urllib.urlopen('http://www.baidu.com')
AttributeError: module 'urllib' has no attribute 'urlopen'
分析:红色的部分表示找不到,一般出现这样的问题有这么几种方式
① 没有引入对应的包,毕竟python的包多如屎
② 有人说是因为你的这个工程目录下可能有一个自己定义的文件与urllib重名,导致上述代码在引用时实际引用的是自定义的那个urllib,结果查找自己的项目文件夹下也没有重名的文件。
但是这个问题就是特别的奇怪,都是不属于上面的情况,在网上查了后原因如下
Python3.X中应该用urllib.request。更改后就不会再出现这个错误了
import urllib.requestdef run_demo(): f=urllib.request.urlopen('http://www.baidu.com') print(f.read())if __name__=='__main__': run_demo()
显示结果如下
b'<!DOCTYPE html>\n<!--STATUS OK-->\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\
- Python 3.x中导入urllib出现AttributeError: module 'urllib' has no attribute 'urlopen'
- Python3.x中出现 "AttributeError: module 'urllib' has no attribute 'urlopen'" 的解决方法
- AttributeError: module 'urllib' has no attribute 'urlopen'
- Python中module 'urllib' has no attribute 'openurl'问题
- Python 2.7.3 AttributeError: 'module' object has no attribute 'urlopen'
- AttributeError: 'module' object has no attribute 'urlopen'
- python错误 module 'urllib' has no attribute 'request'
- Python3.X识别混合编码,顺便解决“AttributeError: 'module' object has no attribute 'urlopen'”
- python3 报错: AttributeError: 'module' object has no attribute 'urlopen'
- python 中出现的AttributeError: 'module' object has no attribute '_handlerList'
- Python基础学习-'module' object has no attribute 'urlopen'解决方法
- AttributeError: 'module' object has no attribute 'handlers'--Python子模块导入问题
- AttributeError 'module' object has no attribute 'handlers'--Python子模块导入问题
- 出现错误如下:AttributeError: 'module' object has no attribute 'SSL_ST_INIT'
- [Python]attributeError:'module' object has no attribute 'dump'
- [python错误]builtins.AttributeError: 'module' object has no attribute 'request'
- python运行不了 AttributeError: module object has no attribute getdefaultlocale
- python AttributeError: 'module' object has no attribute 'setdefaultencoding'
- Water problem HDU
- ob缓存
- nginx 配置手机设备和pc设备请求不同的后台
- 【神经网络与深度学习】【计算机视觉】SPPNet-引入空间金字塔池化改进RCNN
- 关于find命令的一个奇葩问题, 其实不奇葩------不过是软链在捣鬼而已
- Python 3.x中导入urllib出现AttributeError: module 'urllib' has no attribute 'urlopen'
- Water problem HDU
- 设计模式初谈
- openGL学习(一)
- 【神经网络与深度学习】【计算机视觉】Fast R-CNN
- 【计算机网络原理】-学习心得
- 如何撰写发明专利申请文件
- 大话设计模式读书笔记(十二) 抽象工厂模式
- activemq消费者过滤器