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环境下安装使用异步任务队列包Celery的基础教程
May 07 Python
shell命令行,一键创建 python 模板文件脚本方法
Mar 20 Python
python合并同类型excel表格的方法
Apr 01 Python
在python中将字符串转为json对象并取值的方法
Dec 31 Python
python整合ffmpeg实现视频文件的批量转换
May 31 Python
PyQt 实现使窗口中的元素跟随窗口大小的变化而变化
Jun 18 Python
浅谈pycharm使用及设置方法
Sep 09 Python
SpringBoot实现登录注册常见问题解决方案
Mar 04 Python
opencv 图像礼帽和图像黑帽的实现
Jul 07 Python
Python实现封装打包自己写的代码,被python import
Jul 12 Python
Python3爬虫发送请求的知识点实例
Jul 30 Python
Python+Tkinter打造签名设计工具
Apr 01 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面向对象全攻略 (七) 继承性
2009/09/30 PHP
php表单转换textarea换行符的方法
2010/09/10 PHP
深入解析php之sphinx
2013/05/15 PHP
php json转换相关知识(小结)
2018/12/21 PHP
jQuery 选择器理解
2010/03/16 Javascript
jquery.cvtooltip.js 基于jquery的气泡提示插件
2010/11/19 Javascript
同域jQuery(跨)iframe操作DOM(实例讲解)
2013/12/19 Javascript
Javascript图片上传前的本地预览实例
2014/06/16 Javascript
js控制文本框只输入数字和小数点的方法
2015/03/10 Javascript
jQuery实现弹出窗口中切换登录与注册表单
2015/06/05 Javascript
JS截取字符串实例详解
2015/11/24 Javascript
基于jQuery实现左右图片轮播(原理通用)
2015/12/24 Javascript
jQuery简单注册和禁用全局事件的方法
2016/07/25 Javascript
AngularJS 依赖注入详解和简单实例
2016/07/28 Javascript
jquery实现全选、不选、反选的两种方法
2016/09/06 Javascript
详解照片瀑布流效果(js,jquery分别实现与知识点总结)
2017/01/01 Javascript
Vuex之理解state的用法实例
2017/04/19 Javascript
NodeJS实现图片上传代码(Express)
2017/06/30 NodeJs
Python中使用不同编码读写txt文件详解
2015/05/28 Python
Python可变参数函数用法实例
2015/07/07 Python
fastcgi文件读取漏洞之python扫描脚本
2017/04/23 Python
Python字符串处理实现单词反转
2017/06/14 Python
python中requests库session对象的妙用详解
2017/10/30 Python
AI人工智能 Python实现人机对话
2017/11/13 Python
Django中ORM表的创建和增删改查方法示例
2017/11/15 Python
使用Eclipse如何开发python脚本
2018/04/11 Python
Python实现将Excel转换成为image的方法
2018/10/23 Python
Jupyter notebook在mac:linux上的配置和远程访问的方法
2019/01/14 Python
python dict 相同key 合并value的实例
2019/01/21 Python
解决django中ModelForm多表单组合的问题
2019/07/18 Python
UGG雪地靴德国官网:UGG德国
2016/11/19 全球购物
数控专业个人求职信范例
2013/11/29 职场文书
教师一帮一活动总结
2014/07/08 职场文书
《抽屉原理》教学反思
2016/02/20 职场文书
关于JavaScript轮播图的实现
2021/11/20 Javascript
什么是css原子化,有什么用?
2022/04/24 HTML / CSS