Python库urllib与urllib2主要区别分析


Posted in Python onJuly 13, 2014

作为一个Python菜鸟,之前一直懵懂于urllib和urllib2,以为2是1的升级版。今天看到老外写的一篇《Python: difference between urllib and urllib2》才明白其中的区别

You might be intrigued by the existence of two separate URL modules in Python -urllib and urllib2. Even more intriguing: they are not alternatives for each other. So what is the difference between urllib and urllib2, and do we need them both?

你可能对于Python中两个独立存在的-urllib2和-urllib2感到好奇。更有趣的是:它们并不是可以相互代替的。那么这两个模块间的区别是什么,并且这两个我们都需要吗?

urllib and  urllib2are both Python modules that do URL request related stuff but offer different functionalities. Their two most significant differences are listed below:

urllib 和urllib2都是接受URL请求的相关模块,但是提供了不同的功能。两个最显著的不同如下:

•urllib2 can accept a Request object to set the headers for a URL request,urllib accepts only a URL. That means, you cannot masquerade your User Agent string etc.

urllib2可以接受一个Request类的实例来设置URL请求的headers,urllib仅可以接受URL。这意味着,你不可以伪装你的User Agent字符串等。

•urllib provides the urlencode method which is used for the generation of GET query strings, urllib2 doesn't have such a function. This is one of the reasons why urllib is often used along with urllib2.

urllib提供urlencode方法用来GET查询字符串的产生,而urllib2没有。这是为何urllib常和urllib2一起使用的原因。 

 For other differences between urllib and urllib2 refer to their documentations, the links are given in the References section.
Tip: if you are planning to do HTTP stuff only, check out httplib2, it is much better than httplib or urllib or urllib2.

提示:如果你仅做HTTP相关的,看一下httplib2,比其他几个模块好用。

相关资料

这里还有个类似的问答:urllib urllib2 what is the difference ?

stackoverflow上也有:Should I use urllib or urllib2?

Python Docs

  1. urllib
  2. urllib2
Python 相关文章推荐
python计算圆周长、面积、球体体积并画出圆
Apr 08 Python
浅析Python中的赋值和深浅拷贝
Aug 15 Python
Python实现PS图像调整颜色梯度效果示例
Jan 25 Python
PyQt5主窗口动态加载Widget实例代码
Feb 07 Python
python使用opencv驱动摄像头的方法
Aug 03 Python
将string类型的数据类型转换为spark rdd时报错的解决方法
Feb 18 Python
Python操作redis实例小结【String、Hash、List、Set等】
May 16 Python
Python中Unittest框架的具体使用
Aug 27 Python
aws 通过boto3 python脚本打pach的实现方法
May 10 Python
Python 多线程C段扫描、检测 Ping扫描脚本的实现
Sep 03 Python
matplotlib部件之矩形选区(RectangleSelector)的实现
Feb 01 Python
浅析python连接数据库的重要事项
Feb 22 Python
Python文件夹与文件的操作实现代码
Jul 13 #Python
分享15个最受欢迎的Python开源框架
Jul 13 #Python
使用 Python 获取 Linux 系统信息的代码
Jul 13 #Python
Python文件操作类操作实例详解
Jul 11 #Python
Python编写检测数据库SA用户的方法
Jul 11 #Python
python实现的生成随机迷宫算法核心代码分享(含游戏完整代码)
Jul 11 #Python
python文件和目录操作函数小结
Jul 11 #Python
You might like
PHP删除数组中的特定元素的代码
2012/06/28 PHP
PHP+MYSQL中文乱码问题
2015/07/01 PHP
JQuery jsonp 使用示例代码
2009/08/12 Javascript
ASP.NET中基于JQUERY的高性能的TreeView补充
2011/02/23 Javascript
jquery内置验证(validate)使用方法示例(表单验证)
2013/12/04 Javascript
Jquery设置attr的disabled属性控制某行显示或者隐藏
2014/09/25 Javascript
jQuery中prevAll()方法用法实例
2015/01/08 Javascript
jQuery背景插件backstretch使用指南
2015/04/21 Javascript
jQuery+Ajax实现无刷新分页
2015/10/30 Javascript
基于HTML+CSS,jQuery编写的简易计算器后续(添加了键盘监听)
2016/01/05 Javascript
jQuery Validate验证表单时多个name相同的元素只验证第一个的解决方法
2016/12/24 Javascript
深入解析Vue 组件命名那些事
2017/07/18 Javascript
深入浅出webpack教程系列_安装与基本打包用法和命令参数详解
2017/09/10 Javascript
JS判断数组里是否有重复元素的方法小结
2019/05/21 Javascript
javascript获取元素的计算样式
2019/05/24 Javascript
微信小程序去除左上角返回键的实现方法
2020/03/06 Javascript
vue+canvas实现移动端手写签名
2020/05/21 Javascript
jQuery带控制按钮轮播图插件
2020/07/31 jQuery
pytorch + visdom 处理简单分类问题的示例
2018/06/04 Python
Python按钮的响应事件详解
2019/03/04 Python
Django中Middleware中的函数详解
2019/07/18 Python
python opencv调用笔记本摄像头
2019/08/28 Python
Python进程间通信multiprocess代码实例
2020/03/18 Python
Python的3种运行方式:命令行窗口、Python解释器、IDLE的实现
2020/10/10 Python
python3爬虫中引用Queue的实例讲解
2020/11/24 Python
html5构建触屏网站之网站尺寸探讨
2013/01/07 HTML / CSS
意大利消费电子产品购物网站:SLG Store
2019/12/26 全球购物
国际商务专业学生个人的自我评价
2013/09/28 职场文书
人事部主管岗位职责
2013/12/26 职场文书
优秀管理者事迹材料
2014/05/22 职场文书
环境卫生标语
2014/06/09 职场文书
安全生产一岗双责责任书
2014/07/28 职场文书
公安民警正风肃纪剖析材料
2014/10/10 职场文书
警示教育片观后感
2015/06/17 职场文书
Django分页器的用法你都了解吗
2021/05/26 Python
windows11怎么查看wifi密码? win11查看wifi密码的技巧
2021/11/21 数码科技