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 19 Python
python 去除txt文本中的空格、数字、特定字母等方法
Jul 24 Python
在python中使用xlrd获取合并单元格的方法
Dec 26 Python
Django urls.py重构及参数传递详解
Jul 23 Python
python psutil模块使用方法解析
Aug 01 Python
Python判断字符串是否xx开始或结尾的示例
Aug 08 Python
基于python全局设置id 自动化测试元素定位过程解析
Sep 04 Python
pycharm修改file type方式
Nov 19 Python
python多进程并发demo实例解析
Dec 13 Python
python数据爬下来保存的位置
Feb 17 Python
Python类型转换的魔术方法详解
Dec 23 Python
Python中文纠错的简单实现
Jul 07 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/05/09 PHP
php实现mysql备份恢复分卷处理的方法
2014/12/26 PHP
简单理解PHP的面向对象编程方式
2016/05/17 PHP
jQuery LigerUI 插件介绍及使用之ligerDrag和ligerResizable示例代码打包
2011/04/06 Javascript
jQuery多媒体插件jQuery Media Plugin使用详解
2014/12/19 Javascript
js+css实现导航效果实例
2015/02/10 Javascript
jQuery插件jcrop+Fileapi完美实现图片上传+裁剪+预览的代码分享
2015/04/22 Javascript
Angularjs在初始化未完毕时出现闪烁问题的解决方法分析
2016/08/05 Javascript
简单三步实现报表页面集成天气
2016/12/15 Javascript
利用Angular+Angular-Ui实现分页(代码加简单)
2017/03/10 Javascript
jquery实现图片平滑滚动详解
2017/03/22 jQuery
vue2.0实战之基础入门(1)
2017/03/27 Javascript
彻底搞懂JavaScript中的apply和call方法(必看)
2017/09/18 Javascript
详述 Sublime Text 打开 GBK 格式中文乱码的解决方法
2017/10/26 Javascript
微信小程序canvas.drawImage完全显示图片问题的解决
2018/11/30 Javascript
JavaScript基础之静态方法和实例方法分析
2018/12/26 Javascript
详解VUE前端按钮权限控制
2019/04/26 Javascript
vue+swiper实现左右滑动的测试题功能
2020/10/30 Javascript
[42:23]完美世界DOTA2联赛PWL S3 Forest vs Rebirth 第二场 12.10
2020/12/13 DOTA
Python中的集合类型知识讲解
2015/08/19 Python
python json.loads兼容单引号数据的方法
2018/12/19 Python
Django ModelForm操作及验证方式
2020/03/30 Python
使用CSS3的box-sizing属性解决div宽高被内边距撑开的问题
2016/06/28 HTML / CSS
html5 Canvas画图教程(8)—canvas里画曲线之bezierCurveTo方法
2013/01/09 HTML / CSS
html5与css3小应用
2013/04/03 HTML / CSS
AmazeUI 面板的实现示例
2020/08/17 HTML / CSS
英国最红的高街时尚品牌:Topshop
2016/08/05 全球购物
创造美妙香氛体验:Aera扩散器和香水
2018/11/25 全球购物
办公室驾驶员岗位职责
2013/11/15 职场文书
小学生获奖感言范文
2014/02/02 职场文书
信息管理与信息系统专业求职信
2014/06/21 职场文书
美术课外活动总结
2014/07/08 职场文书
财务科长个人对照检查材料
2014/09/18 职场文书
2015年党员个人工作总结
2015/05/13 职场文书
2016道德模范先进事迹材料
2016/02/26 职场文书
MySQL数据库之内置函数和自定义函数 function
2022/06/16 MySQL