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中使用百度音乐搜索的api下载指定歌曲的lrc歌词
Jul 18 Python
Python中字典和集合学习小结
Jul 07 Python
Python3.5基础之NumPy模块的使用图文与实例详解
Apr 24 Python
Django 多表关联 存储 使用方法详解 ManyToManyField save
Aug 09 Python
python实现批量修改服务器密码的方法
Aug 13 Python
python如何保证输入键入数字的方法
Aug 23 Python
python3中rank函数的用法
Nov 27 Python
django queryset相加和筛选教程
May 18 Python
python如何写出表白程序
Jun 01 Python
详解python中GPU版本的opencv常用方法介绍
Jul 24 Python
Python 如何实现访问者模式
Jul 28 Python
Python中的None与 NULL(即空字符)的区别详解
Sep 24 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
Thinkphp框架 表单自动验证登录注册 ajax自动验证登录注册
2016/12/27 PHP
php传值和传引用的区别点总结
2019/11/19 PHP
一个不错的用JavaScript实现的UBB编码函数
2007/03/09 Javascript
json2.js的初步学习与了解
2011/10/06 Javascript
Jquery实现Div上下移动示例
2014/04/23 Javascript
node.js中的fs.mkdirSync方法使用说明
2014/12/17 Javascript
浅谈JS中逗号运算符的用法
2016/06/12 Javascript
老生常谈JQuery data方法的使用
2016/09/09 Javascript
JS弹性运动实现方法分析
2016/12/15 Javascript
谈谈VUE种methods watch和compute的区别和联系
2017/08/01 Javascript
简单谈谈CommonsChunkPlugin抽取公共模块
2017/12/31 Javascript
利用JS实现一个同Excel表现的智能填充算法
2018/08/13 Javascript
解决vue 引入子组件报错的问题
2018/09/06 Javascript
JavaScript循环遍历你会用哪些之小结篇
2018/09/28 Javascript
js canvas实现橡皮擦效果
2018/12/20 Javascript
Vue之封装公用变量以及实现方式
2020/07/31 Javascript
[02:45]DOTA2英雄基础教程 伐木机
2013/12/23 DOTA
Python3基础之输入和输出实例分析
2014/08/18 Python
python中类变量与成员变量的使用注意点总结
2017/04/29 Python
Python使用wget实现下载网络文件功能示例
2018/05/31 Python
Scrapy框架爬取西刺代理网免费高匿代理的实现代码
2019/02/22 Python
numpy.linspace函数具体使用详解
2019/05/27 Python
python实现桌面气泡提示功能
2019/07/29 Python
Python计算两个矩形重合面积代码实例
2019/09/16 Python
Python 解决OPEN读文件报错 ,路径以及r的问题
2019/12/19 Python
Python3使用腾讯云文字识别(腾讯OCR)提取图片中的文字内容实例详解
2020/02/18 Python
python如何随机生成高强度密码
2020/08/19 Python
工商学院毕业生自荐信
2013/11/12 职场文书
2014年医学生毕业自我鉴定
2014/03/26 职场文书
毕业生实习证明
2014/09/19 职场文书
六一儿童节新闻稿
2015/07/17 职场文书
2016年小学生寒假家长评语
2015/10/10 职场文书
2016年教师党员承诺书范文
2016/03/24 职场文书
pytorch 预训练模型读取修改相关参数的填坑问题
2021/06/05 Python
MYSQL 的10大经典优化案例场景实战
2021/09/14 MySQL
Spring Cloud OAuth2实现自定义token返回格式
2022/06/25 Java/Android