解决python3 urllib中urlopen报错的问题


Posted in Python onMarch 25, 2017

前言

最近更新了Python版本,准备写个爬虫,意外的发现urllib库中属性不存在urlopen,于是各种google,然后总结一下给出解决方案

问题的出现

解决python3 urllib中urlopen报错的问题

AttributeError: 'module' object has no attribute 'urlopen'

问题的解决途径

我们先来看下官方文档的解释:

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.)

也就是说官方3.0版本已经把urllib2,urlparse等五个模块都并入了urllib中,也就是整合了。

正确的使用方法

import urllib.request 
url="http://www.baidu.com" 
get=urllib.request.urlopen(url).read() 
print(get)

结果示意图:

解决python3 urllib中urlopen报错的问题 

其实也是可以换个utf-8的编码让读取出来的源码更正确的,但这已经是番外的不再提了。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对三水点靠木的支持。

Python 相关文章推荐
Python 命令行参数sys.argv
Sep 06 Python
python检测远程端口是否打开的方法
Mar 14 Python
Django中模版的子目录与include标签的使用方法
Jul 16 Python
每天迁移MySQL历史数据到历史库Python脚本
Apr 13 Python
Python随机函数random()使用方法小结
Apr 29 Python
python查找指定文件夹下所有文件并按修改时间倒序排列的方法
Oct 21 Python
python+opencv实现阈值分割
Dec 26 Python
python3使用腾讯企业邮箱发送邮件的实例
Jun 28 Python
python的几种矩阵相乘的公式详解
Jul 10 Python
python生成requirements.txt的两种方法
Sep 18 Python
Python ckeditor富文本编辑器代码实例解析
Jun 22 Python
django中ImageField的使用详解
Dec 21 Python
Python制作Windows系统服务
Mar 25 #Python
Python 类的继承实例详解
Mar 25 #Python
python利用拉链法实现字典方法示例
Mar 25 #Python
python3实现ftp服务功能(服务端 For Linux)
Mar 24 #Python
python3实现ftp服务功能(客户端)
Mar 24 #Python
Python 中urls.py:URL dispatcher(路由配置文件)详解
Mar 24 #Python
python 类详解及简单实例
Mar 24 #Python
You might like
ecshop 订单确认中显示省市地址信息的方法
2010/03/15 PHP
PHP 如何利用phpexcel导入数据库
2013/08/24 PHP
php发送html格式文本邮件的方法
2015/06/10 PHP
PHP处理数组和XML之间的互相转换
2016/06/02 PHP
POST一个JSON格式的数据给Restful服务实例详解
2017/04/07 PHP
jQuery 第二课 操作包装集元素代码
2010/03/14 Javascript
Js基础学习资料
2010/11/23 Javascript
基于jQuery的获取标签名的代码
2012/07/16 Javascript
js输出阴历、阳历、年份、月份、周示例代码
2014/01/29 Javascript
jQuery的图片滑块焦点图插件整理推荐
2014/12/07 Javascript
JS表的模拟方法
2015/02/05 Javascript
javascript编写贪吃蛇游戏
2015/07/07 Javascript
JS实现仿FLASH效果的竖排导航代码
2015/09/15 Javascript
js与applet相互调用的方法
2016/06/22 Javascript
jQuery EasyUI常用数据验证汇总
2016/09/18 Javascript
Javascript typeof与instanceof的区别
2016/10/18 Javascript
js实现常见的工具条效果
2017/03/02 Javascript
jQuery实现获取h1-h6标题元素值的方法
2017/03/06 Javascript
微信小程序上滑加载下拉刷新(onscrollLower)分批加载数据(一)
2017/05/11 Javascript
推荐几个不错的console调试技巧实现
2019/12/20 Javascript
vue keep-alive的简单总结
2021/01/25 Vue.js
利用python模拟sql语句对员工表格进行增删改查
2017/07/05 Python
Python GUI编程完整示例
2019/04/04 Python
Python实现二叉树前序、中序、后序及层次遍历示例代码
2019/05/18 Python
如何用Python破解wifi密码过程详解
2019/07/12 Python
Python绘图实现显示中文
2019/12/04 Python
Python生成随机验证码代码实例解析
2020/06/09 Python
佳能加拿大网上商店:Canon eStore Canada
2018/04/04 全球购物
巴西手表购物网站:eclock
2019/03/19 全球购物
奢华的意大利皮革手袋:Bene Handbags
2019/10/29 全球购物
大学生新闻专业个人自我评价
2013/11/12 职场文书
化工专业个人的求职信范文
2013/11/28 职场文书
五年级下册复习计划
2015/01/19 职场文书
2015年简历自我评价范文
2015/03/11 职场文书
寻找最美乡村教师观后感
2015/06/18 职场文书
Mysql多层子查询示例代码(收藏夹案例)
2022/03/31 MySQL