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基于二分查找实现求整数平方根的方法
May 12 Python
基于Python的接口测试框架实例
Nov 04 Python
利用python获取某年中每个月的第一天和最后一天
Dec 15 Python
python实现下载文件的三种方法
Feb 09 Python
python机器学习之神经网络(二)
Dec 20 Python
彻彻底底地理解Python中的编码问题
Oct 15 Python
通过shell+python实现企业微信预警
Mar 07 Python
python连接、操作mongodb数据库的方法实例详解
Sep 11 Python
python cv2截取不规则区域图片实例
Dec 21 Python
jupyter notebook中美观显示矩阵实例
Apr 17 Python
Python控制鼠标键盘代码实例
Dec 08 Python
Python语法学习之进程的创建与常用方法详解
Apr 08 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操作数组的一些函数整理介绍
2011/07/17 PHP
PHP采用超长(超大)数字运算防止数字以科学计数法显示的方法
2016/04/01 PHP
PHP线程的内存回收问题
2016/07/08 PHP
PHP7引入的"??"和"?:"的区别讲解
2019/04/08 PHP
基于JQuery 选择器使用说明介绍
2013/04/18 Javascript
Jquery 模板数据绑定插件的使用方法详解
2013/07/08 Javascript
一个获取第n个元素节点的js函数
2014/09/02 Javascript
浅析javascript异步执行函数导致的变量变化问题解决思路
2016/05/13 Javascript
JS去掉字符串前后空格、阻止表单提交的实现代码
2017/06/08 Javascript
JavaScript订单操作小程序完整版
2017/06/23 Javascript
Vue自定义指令使用方法详解
2017/08/21 Javascript
VUE中的无限循环代码解析
2017/09/22 Javascript
Vuejs 单文件组件实例详解
2018/02/09 Javascript
JavaScript动态检测密码强度原理及实现方法详解
2019/06/11 Javascript
Vuex的实战使用详解
2019/10/31 Javascript
Node Mongoose用法详解【Mongoose使用、Schema、对象、model文档等】
2020/05/13 Javascript
利用python实现简单的邮件发送客户端示例
2017/12/23 Python
python实现windows壁纸定期更换功能
2019/01/21 Python
python使用配置文件过程详解
2019/12/28 Python
Django CSRF认证的几种解决方案
2020/03/03 Python
利用python绘制数据曲线图的实现
2020/04/09 Python
Python从文件中读取数据的方法步骤
2020/11/18 Python
CSS3 实现穿梭星空动画
2020/11/13 HTML / CSS
Html5+CSS3+EL表达式问题小结
2020/12/19 HTML / CSS
西班牙床垫网上商店:Colchones.es
2018/05/06 全球购物
学生请假条
2014/04/11 职场文书
目标责任书格式
2014/07/28 职场文书
党员学习中共十八大思想报告
2014/09/12 职场文书
党的群众路线教育实践活动督导组工作情况汇报
2014/10/28 职场文书
2014年服务行业工作总结
2014/11/18 职场文书
2015年世界环境日演讲稿
2015/03/18 职场文书
新学期感想
2015/08/10 职场文书
宝宝满月祝酒词
2015/08/10 职场文书
升学宴祝酒词
2015/08/11 职场文书
python实现三阶魔方还原的示例代码
2021/04/28 Python
如何理解python接口自动化之logging日志模块
2021/06/15 Python