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之一个免费的实验室
Sep 14 Python
Python selenium 父子、兄弟、相邻节点定位方式详解
Sep 15 Python
Python使用中文正则表达式匹配指定中文字符串的方法示例
Jan 20 Python
Python装饰器用法示例小结
Feb 11 Python
Python实现的直接插入排序算法示例
Apr 29 Python
Random 在 Python 中的使用方法
Aug 09 Python
Python基础知识点 初识Python.md
May 14 Python
使用Matplotlib 绘制精美的数学图形例子
Dec 13 Python
Python基于模块Paramiko实现SSHv2协议
Apr 28 Python
python 实现mysql自动增删分区的方法
Apr 01 Python
CocosCreator ScrollView优化系列之分帧加载
Apr 14 Python
Python超详细分步解析随机漫步
Mar 17 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 SPL标准库中的常用函数介绍
2015/05/11 PHP
浅谈PHP eval()函数定义和用法
2016/06/21 PHP
php中this关键字用法分析
2016/12/07 PHP
php实现将二维关联数组转换成字符串的方法详解
2017/07/31 PHP
字符串的replace方法应用浅析
2011/12/06 Javascript
JavaScript不使用prototype和new实现继承机制
2014/12/29 Javascript
js判断浏览器版本以及浏览器内核的方法
2015/01/20 Javascript
基于JavaScript实现动态创建表格和增加表格行数
2015/12/20 Javascript
深入理解node.js之path模块
2017/05/03 Javascript
jQuery+C#实现参数RSA加密传输功能【附jsencrypt.js下载】
2017/06/26 jQuery
js 事件的传播机制(实例讲解)
2017/07/20 Javascript
Vue 组件间的样式冲突污染
2017/08/31 Javascript
微信小程序实现全国机场索引列表
2018/01/31 Javascript
angularJS1 url中携带参数的获取方法
2018/10/09 Javascript
webpack4.x CommonJS模块化浅析
2018/11/09 Javascript
如何用RxJS实现Redux Form
2018/12/29 Javascript
JavaScript中变量提升机制示例详解
2019/12/27 Javascript
Vue+webpack实现懒加载过程解析
2020/02/17 Javascript
vue 导航锚点_点击平滑滚动,导航栏对应变化详解
2020/08/10 Javascript
Python实现批量下载图片的方法
2015/07/08 Python
Python实现数据库并行读取和写入实例
2017/06/09 Python
python利用MethodType绑定方法到类示例代码
2017/08/27 Python
python 寻找list中最大元素对应的索引方法
2018/06/28 Python
python 多线程对post请求服务器测试并发的方法
2019/06/13 Python
详解Python 重学requests发起请求的基本方式
2020/02/07 Python
python按照list中字典的某key去重的示例代码
2020/10/13 Python
python3 kubernetes api的使用示例
2021/01/12 Python
HTML5新增的8类INPUT输入类型介绍
2015/07/06 HTML / CSS
为中国消费者甄选天下优品:网易严选
2016/08/11 全球购物
美国最大的烧烤架和户外生活用品专业零售商:Barbeques Galore
2021/01/09 全球购物
屈臣氏泰国官网:Watsons TH
2021/02/23 全球购物
酒店服务实习自我鉴定
2013/09/22 职场文书
大学生新学期计划书
2014/04/28 职场文书
社区党的群众路线教育实践活动剖析材料
2014/10/09 职场文书
群众路线教育实践活动学习笔记内容
2014/11/06 职场文书
小学班主任心得体会
2016/01/07 职场文书