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通过pil模块获得图片exif信息的方法
Mar 16 Python
python统计文本文件内单词数量的方法
May 30 Python
Python面向对象class类属性及子类用法分析
Feb 02 Python
详解Python最长公共子串和最长公共子序列的实现
Jul 07 Python
python  创建一个保留重复值的列表的补码
Oct 15 Python
Python函数定义及传参方式详解(4种)
Mar 18 Python
如何爬取通过ajax加载数据的网站
Aug 15 Python
python多线程实现TCP服务端
Sep 03 Python
使用python去除图片白色像素的实例
Dec 12 Python
Tensorflow矩阵运算实例(矩阵相乘,点乘,行/列累加)
Feb 05 Python
PyQt5实现仿QQ贴边隐藏功能的实例代码
May 24 Python
python3.6.5基于kerberos认证的hive和hdfs连接调用方式
Jun 06 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
深入了解php4(1)--回到未来
2006/10/09 PHP
PHP5+UTF8多文件上传类
2008/10/17 PHP
ThinkPHP令牌验证实例
2014/06/18 PHP
PHP根据两点间的经纬度计算距离
2014/10/31 PHP
php无序树实现方法
2015/07/28 PHP
自制PHP框架之设计模式
2017/05/07 PHP
详解PHP中mb_strpos的使用
2018/02/04 PHP
PHP 观察者模式深入理解与应用分析
2019/09/25 PHP
javascript 有趣而诡异的数组
2009/04/06 Javascript
AJAX分页的代码(后台asp.net)
2011/02/14 Javascript
Javascript字符串对象的常用方法简明版
2014/06/26 Javascript
JS实现alert中显示换行的方法
2015/12/17 Javascript
nodeJs爬虫获取数据简单实现代码
2016/03/29 NodeJs
Vue过滤器的用法和自定义过滤器使用
2017/02/08 Javascript
ES6学习教程之块级作用域详解
2017/10/09 Javascript
JavaScript引用类型RegExp基本用法详解
2018/08/09 Javascript
Node4-5静态资源服务器实战以及优化压缩文件实例内容
2019/08/29 Javascript
超详细小程序定位地图模块全系列开发教学
2020/11/24 Javascript
[02:05]2014DOTA2西雅图邀请赛 老队长全明星大猜想谁不服就按进显示器
2014/07/08 DOTA
[01:20]辉夜杯背景故事宣传片《辉夜传说》
2015/12/25 DOTA
[00:15]TI9地铁玩家打卡
2019/08/11 DOTA
Tensorflow 实现修改张量特定元素的值方法
2018/07/30 Python
python实现播放音频和录音功能示例代码
2018/12/30 Python
Python面向对象程序设计之类的定义与继承简单示例
2019/03/18 Python
Python实现的服务器示例小结【单进程、多进程、多线程、非阻塞式】
2019/05/23 Python
Python批量修改图片分辨率的实例代码
2019/07/04 Python
python能在浏览器能运行吗
2020/06/17 Python
Python3中的tuple函数知识点讲解
2021/01/03 Python
Python实现简单猜数字游戏
2021/02/03 Python
AE美国鹰美国官方网站:American Eagle Outfitters
2016/08/22 全球购物
麦当劳印度网上订餐:McDelivery
2020/03/16 全球购物
活动倡议书范文
2014/05/13 职场文书
银行求职信范文
2019/05/13 职场文书
你真的了解PHP中的引用符号(&)吗
2021/05/12 PHP
css让页脚保持在底部位置的四种方案
2022/07/23 HTML / CSS
Python pyecharts案例超市4年数据可视化分析
2022/08/14 Python