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中的RSS处理
Apr 13 Python
Python的Django框架中的Context使用
Jul 15 Python
python如何查看系统网络流量的信息
Sep 12 Python
浅谈Python爬取网页的编码处理
Nov 04 Python
在django中实现页面倒数几秒后自动跳转的例子
Aug 16 Python
使用pandas实现连续数据的离散化处理方式(分箱操作)
Nov 22 Python
Python实现投影法分割图像示例(二)
Jan 17 Python
Python如何通过Flask-Mail发送电子邮件
Jan 29 Python
python怎么对数字进行过滤
Jul 05 Python
用python批量解压带密码的压缩包
May 31 Python
pytorch model.cuda()花费时间很长的解决
Jun 01 Python
Python anaconda安装库命令详解
Oct 16 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实现框架(一)
2006/10/09 PHP
Warning: session_destroy() : Trying to destroy uninitialized sessionq错误
2011/06/16 PHP
使用php实现快钱支付功能(涉及到接口)
2013/07/01 PHP
ThinkPHP表单自动验证实例
2014/10/13 PHP
php自定义urlencode,urldecode函数实例
2015/03/24 PHP
php实现自定义中奖项数和概率的抽奖函数示例
2017/05/26 PHP
js用图作提交按钮或超连接
2008/03/26 Javascript
异步动态加载JS并运行(示例代码)
2013/12/13 Javascript
类似天猫商品详情随浏览器移动的示例代码
2014/02/27 Javascript
javascript中的循环语句for语句深入理解
2014/04/04 Javascript
JS获得浏览器版本和操作系统版本的例子
2014/05/13 Javascript
浅析AngularJS中的生命周期和延迟处理
2015/06/18 Javascript
jQuery Timelinr实现垂直水平时间轴插件(附源码下载)
2016/02/16 Javascript
深入理解js中的加载事件
2017/02/08 Javascript
vue.js 获取当前自定义属性值
2017/06/01 Javascript
JavaScript-定时器0~9抽奖系统详解(代码)
2017/08/16 Javascript
解决JSON.stringify()自动将中文转译成unicode的问题
2018/01/05 Javascript
vue+jquery+lodash实现滑动时顶部悬浮固定效果
2018/04/28 jQuery
解决Vue2.0中使用less给元素添加背景图片出现的问题
2018/09/03 Javascript
python回调函数用法实例分析
2015/05/09 Python
Python的多维空数组赋值方法
2018/04/13 Python
如何在Django项目中引入静态文件
2019/07/26 Python
Python hashlib模块加密过程解析
2019/11/05 Python
python实现12306登录并保存cookie的方法示例
2019/12/17 Python
python实现ssh及sftp功能(实例代码)
2020/03/16 Python
Python3+selenium实现cookie免密登录的示例代码
2020/03/18 Python
python利用google翻译方法实例(翻译字幕文件)
2020/09/21 Python
英国领先的男士美容护发用品公司:Mankind
2016/08/31 全球购物
G-Form护具官方网站:美国运动保护装备
2019/09/04 全球购物
购买原创艺术品:Zatista
2019/11/09 全球购物
竞选班委演讲稿
2014/04/28 职场文书
2015年民主评议党员工作总结
2015/05/19 职场文书
2016创先争优活动党员公开承诺书
2016/03/24 职场文书
2016年大学生社区服务活动总结
2016/04/06 职场文书
JavaWeb 入门:Hello Servlet
2021/07/16 Java/Android
win10+RTX3050ti+TensorFlow+cudn+cudnn配置深度学习环境的方法
2022/06/25 Servers