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基于twisted实现简单的web服务器
Sep 29 Python
python统计文本文件内单词数量的方法
May 30 Python
Python中的数学运算操作符使用进阶
Jun 20 Python
ansible作为python模块库使用的方法实例
Jan 17 Python
Python3.6简单操作Mysql数据库
Sep 12 Python
python之Character string(实例讲解)
Sep 25 Python
Python中elasticsearch插入和更新数据的实现方法
Apr 01 Python
Python提取转移文件夹内所有.jpg文件并查看每一帧的方法
Jun 27 Python
python 读取修改pcap包的例子
Jul 23 Python
python字符串替换re.sub()方法解析
Sep 18 Python
python环境下安装opencv库的方法
Mar 05 Python
在 Golang 中实现 Cache::remember 方法详解
Mar 30 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
苏联队长,苏联超人蝙蝠侠,这些登场的“山寨”英雄真的很严肃
2020/04/09 欧美动漫
php实现数组筛选奇数和偶数示例
2014/04/11 PHP
PHP测试成功的邮件发送案例
2015/10/26 PHP
PHP类型约束用法示例
2016/09/28 PHP
PHP微信红包生成代码分享
2016/10/06 PHP
Nigma vs Alliance BO5 第一场2.14
2021/03/10 DOTA
自己的js工具 Event封装
2009/08/21 Javascript
jquery动态分页效果堪比时光网
2014/09/25 Javascript
JavaScript数组常用操作技巧汇总
2014/11/17 Javascript
JS实现的在线调色板实例(附demo源码下载)
2016/03/01 Javascript
基于jQuery日历插件制作日历
2016/03/11 Javascript
DIV+CSS+jQ实现省市联动可扩展
2016/06/22 Javascript
Bootstrap源码解读排版(1)
2016/12/23 Javascript
laydate只显示时分 不显示秒的功能实现方法
2019/09/28 Javascript
react实现复选框全选和反选组件效果
2020/08/25 Javascript
python3使用tkinter实现ui界面简单实例
2014/01/10 Python
零基础写python爬虫之使用urllib2组件抓取网页内容
2014/11/04 Python
跟老齐学Python之使用Python操作数据库(1)
2014/11/25 Python
Python中正则表达式的详细教程
2015/04/30 Python
使用C#配合ArcGIS Engine进行地理信息系统开发
2016/02/19 Python
python调用tcpdump抓包过滤的方法
2018/07/18 Python
PySide和PyQt加载ui文件的两种方法
2019/02/27 Python
Python实现的企业粉丝抽奖功能示例
2019/07/26 Python
Python TCP通信客户端服务端代码实例
2019/11/21 Python
Python爬取腾讯视频评论的思路详解
2019/12/19 Python
python sitk.show()与imageJ结合使用常见的问题
2020/04/20 Python
应用数学自荐书范文
2013/11/24 职场文书
新郎婚宴答谢词
2014/01/19 职场文书
自荐书范文范例
2014/02/13 职场文书
《歌唱二小放牛郎》教学反思
2014/04/19 职场文书
群众路线教育实践活动心得体会(四风)
2014/11/03 职场文书
运动会开幕式致辞
2015/07/29 职场文书
留学文书中的个人陈述,应该注意哪些问题?
2019/08/23 职场文书
go语言map与string的相互转换的实现
2021/04/07 Golang
Python深度学习之Pytorch初步使用
2021/05/20 Python
【海涛dota解说】海涛小满开黑4v5被破两路翻盘潮汐第一视角解说
2022/04/01 DOTA