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实现ftp客户端示例分享
Feb 17 Python
python使用BeautifulSoup分析网页信息的方法
Apr 04 Python
在Python中使用成员运算符的示例
May 13 Python
Python从MP3文件获取id3的方法
Jun 15 Python
详解Python3中ceil()函数用法
Feb 19 Python
使用Python给头像戴上圣诞帽的图像操作过程解析
Sep 20 Python
python计算无向图节点度的实例代码
Nov 22 Python
Python 实现敏感目录扫描的示例代码
May 21 Python
Python实现在线批量美颜功能过程解析
Jun 10 Python
Python接口测试环境搭建过程详解
Jun 29 Python
Python实现简单的猜单词小游戏
Oct 28 Python
python区块链实现简版工作量证明
May 25 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设计模式之观察者模式的应用详解
2013/05/21 PHP
PHP计算加权平均数的方法
2015/07/16 PHP
javascript 函数式编程
2007/08/16 Javascript
ie和firefox不兼容的解决方法集合
2009/04/28 Javascript
javaScript 动态访问JSon元素示例代码
2013/08/30 Javascript
jQuery中detach()方法用法实例
2014/12/25 Javascript
javascript数组去重的方法汇总
2015/04/14 Javascript
JS实现统计复选框选中个数并提示确定与取消的方法
2015/07/01 Javascript
JavaScript去除数组里重复值的方法
2015/07/13 Javascript
js仿苹果iwatch外观的计时器代码分享
2015/08/26 Javascript
Bootstrap缩略图与警告框学习使用
2017/02/08 Javascript
vue如何从接口请求数据
2017/06/22 Javascript
Angular将填入表单的数据渲染到表格的方法
2017/09/22 Javascript
简单了解Ajax表单序列化的实现方法
2019/06/14 Javascript
JS/jQuery实现超简单的Table表格添加,删除行功能示例
2019/07/31 jQuery
Vue+Node实现的商城用户管理功能示例
2019/12/23 Javascript
[01:31]DOTA2上海特级锦标赛 SECRET战队完整宣传片
2016/03/16 DOTA
[38:39]完美世界DOTA2联赛循环赛 IO vs GXR BO2第二场 11.04
2020/11/05 DOTA
python 快速排序代码
2009/11/23 Python
python输出指定月份日历的方法
2015/04/23 Python
在Django框架中设置语言偏好的教程
2015/07/27 Python
Python安装图文教程 Pycharm安装教程
2018/03/27 Python
pandas重新生成索引的方法
2018/11/06 Python
Django渲染Markdown文章目录的方法示例
2019/01/02 Python
python 定时器,实现每天凌晨3点执行的方法
2019/02/20 Python
django-rest-framework 自定义swagger过程详解
2019/07/18 Python
python词云库wordCloud使用方法详解(解决中文乱码)
2020/02/17 Python
python入门教程之基本算术运算符
2020/11/13 Python
联想马亚西亚官方网站:Lenovo Malaysia
2018/09/19 全球购物
毕业生多媒体设计求职信
2013/10/12 职场文书
四个太阳教学反思
2014/02/01 职场文书
《都江堰》教学反思
2014/02/07 职场文书
十八届三中全会学习方案
2014/02/16 职场文书
2014年服务行业工作总结
2014/11/18 职场文书
2014年财务个人工作总结
2014/12/08 职场文书
提档介绍信范文
2015/10/22 职场文书