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常用的日期时间处理方法示例
Feb 08 Python
Python常用库推荐
Dec 04 Python
Python+matplotlib实现华丽的文本框演示代码
Jan 22 Python
Python命令行解析模块详解
Feb 01 Python
tensorflow训练中出现nan问题的解决
Feb 10 Python
Python代码打开本地.mp4格式文件的方法
Jan 03 Python
利用python实现对web服务器的目录探测的方法
Feb 26 Python
Python jieba库用法及实例解析
Nov 04 Python
Django-simple-captcha验证码包使用方法详解
Nov 28 Python
Python+MySQL随机试卷及答案生成程序的示例代码
Feb 01 Python
【超详细】八大排序算法的各项比较以及各自特点
Mar 31 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脚本
2006/11/26 PHP
php数组函数序列之each() - 获取数组当前内部指针所指向元素的键名和键值,并将指针移到下一位
2011/10/31 PHP
PHP大批量插入数据库的3种方法和速度对比
2014/07/08 PHP
3款值得推荐的微信开发开源框架
2014/10/28 PHP
php中的依赖注入实例详解
2019/08/14 PHP
Yii框架数据库查询、增加、删除操作示例
2019/10/14 PHP
php实现通过stomp协议连接ActiveMQ操作示例
2020/02/23 PHP
PHP网站常见安全漏洞,及相应防范措施总结
2021/03/01 PHP
js option删除代码集合
2008/11/12 Javascript
javascript中的prototype属性使用说明(函数功能扩展)
2010/08/16 Javascript
JavaScript对象数组排序函数及六个用法
2015/12/23 Javascript
JS 动态加载js文件和css文件 同步/异步的两种简单方式
2016/09/23 Javascript
javaScript嗅探执行神器-sniffer.js
2017/02/14 Javascript
Javascript实现从小到大的数组转换成二叉搜索树
2017/06/13 Javascript
vue使用自定义指令实现拖拽
2021/01/29 Javascript
angularjs请求数据的方法示例
2019/08/06 Javascript
package.json配置文件构成详解
2019/08/27 Javascript
在vue中使用el-tab-pane v-show/v-if无效的解决
2020/08/03 Javascript
import的本质解析
2017/10/30 Python
PyCharm在win10的64位系统安装实例
2017/11/26 Python
python进行两个表格对比的方法
2018/06/27 Python
如何在python字符串中输入纯粹的{}
2018/08/22 Python
Python 处理日期时间的Arrow库使用
2020/08/18 Python
python 爬取腾讯视频评论的实现步骤
2021/02/18 Python
美国领先的宠物用品和宠物食品零售商:Petco
2020/10/28 全球购物
家长给老师的道歉信
2014/01/13 职场文书
前厅收银主管岗位职责
2014/02/04 职场文书
农村结婚典礼司仪主持词
2014/03/14 职场文书
创建青年文明号材料
2014/05/09 职场文书
文艺演出策划方案
2014/06/07 职场文书
2014迎接教师节演讲稿
2014/09/10 职场文书
员工工作自我评价
2014/09/26 职场文书
公司员工宿舍管理制度
2015/08/03 职场文书
如何使用vue3打造一个物料库
2021/05/08 Vue.js
MongoDB数据库常用的10条操作命令
2021/06/18 MongoDB
MIME类型中application/xml与text/xml的区别介绍
2022/01/18 HTML / CSS