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删除文件示例分享
Jan 28 Python
用Python编写一个每天都在系统下新建一个文件夹的脚本
May 04 Python
python获取当前时间对应unix时间戳的方法
May 15 Python
Python制作简单的网页爬虫
Nov 22 Python
python列表的增删改查实例代码
Jan 30 Python
python使用KNN算法手写体识别
Feb 01 Python
Python功能点实现:函数级/代码块级计时器
Jan 02 Python
python网络爬虫 CrawlSpider使用详解
Sep 27 Python
django商品分类及商品数据建模实例详解
Jan 03 Python
Python+OpenCV实现旋转文本校正方式
Jan 09 Python
pytorch模型存储的2种实现方法
Feb 14 Python
Django与AJAX实现网页动态数据显示的示例代码
Feb 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
php弹出对话框实现重定向代码
2014/01/23 PHP
PHP is_array() 检测变量是否是数组的实现方法
2016/06/13 PHP
Symfony2获取web目录绝对路径、相对路径、网址的方法
2016/11/14 PHP
PHP如何使用JWT做Api接口身份认证的实现
2020/02/03 PHP
解决 firefox 不支持 document.all的方法
2007/03/12 Javascript
可自己添加html的伪弹出框实现代码
2013/09/08 Javascript
浅析IE10兼容性问题(frameset的cols属性)
2014/01/03 Javascript
Treegrid的动态加载实例代码
2016/04/29 Javascript
jQuery Validate插件实现表单验证
2016/08/19 Javascript
vue.js学习之递归组件
2016/12/13 Javascript
微信小程序实现图片压缩功能
2018/01/26 Javascript
Vue导出页面为PDF格式的实现思路
2018/07/31 Javascript
Vue实现6位数密码效果
2018/08/18 Javascript
原生JS实现自定义下拉单选选择框功能
2018/10/12 Javascript
vue中使用百度脑图kityminder-core二次开发的实现
2019/09/26 Javascript
JavaScript setInterval()与setTimeout()计时器
2019/12/27 Javascript
[03:17]2016完美“圣”典风云人物:冷冷专访
2016/12/08 DOTA
深入探究Django中的Session与Cookie
2017/07/30 Python
python使用Tkinter实现在线音乐播放器
2018/01/30 Python
Python输入二维数组方法
2018/04/13 Python
numpy添加新的维度:newaxis的方法
2018/08/02 Python
Python3.6.x中内置函数总结及讲解
2019/02/22 Python
python把转列表为集合的方法
2019/06/28 Python
简单了解Django应用app及分布式路由
2019/07/24 Python
python实现kNN算法识别手写体数字的示例代码
2019/08/16 Python
Python获取指定网段正在使用的IP
2020/12/14 Python
HTML5之SVG 2D入门7—SVG元素的重用与引用
2013/01/30 HTML / CSS
英国手机零售商:Carphone Warehouse
2018/06/06 全球购物
戴尔英国翻新电脑和电子产品:Dell UK Refurbished Computers
2019/07/30 全球购物
俄罗斯金苹果网上化妆品和香水商店:Goldapple
2019/12/01 全球购物
利用promise及参数解构封装ajax请求的方法
2021/03/24 Javascript
机关出纳岗位职责
2014/04/03 职场文书
2014年家长学校工作总结
2014/11/20 职场文书
签字仪式主持词
2015/07/03 职场文书
学术会议开幕词
2016/03/03 职场文书
Python自动化工具之实现Excel转Markdown表格
2022/04/08 Python