python2.7:urllib 和urllib2之间的区别
2016-03-25 10:25
579 查看
You
might be intrigued by the existence of two separate URL modules in Python -
Even more intriguing: they are not alternatives for each other. So what is the difference between
and do we need them both?
[align=justify][/align]
你可能对于Python中两个独立存在的-urllib2和-urllib2感到好奇。更有趣的是:它们并不是可以相互代替的。那么这两个模块间的区别是什么,并且这两个我们都需要吗?
both Python modules that do URL request related stuff but offer different functionalities. Their two most significant differences are listed below:
urllib 和urllib2都是接受URL请求的相关模块,但是提供了不同的功能。两个最显著的不同如下:
to set the headers for a URL request,
urllib2可以接受一个Request类的实例来设置URL请求的headers,urllib仅可以接受URL。这意味着,你不可以伪装你的User Agent字符串等。
[align=justify][/align]
which is used for the generation of GET query strings,
why
urllib提供urlencode方法用来GET查询字符串的产生,而urllib2没有。这是为何urllib常和urllib2一起使用的原因。
[align=justify][/align]
[align=justify][/align]
For other differences between
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://www.hacksparrow.com/python-difference-between-urllib-and-urllib2.html http://www.hacksparrow.com/python-difference-between-urllib-and-urllib2.html
might be intrigued by the existence of two separate URL modules in Python -
urlliband
urllib2.
Even more intriguing: they are not alternatives for each other. So what is the difference between
urlliband
urllib2,
and do we need them both?
[align=justify][/align]
你可能对于Python中两个独立存在的-urllib2和-urllib2感到好奇。更有趣的是:它们并不是可以相互代替的。那么这两个模块间的区别是什么,并且这两个我们都需要吗?
urlliband 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请求的相关模块,但是提供了不同的功能。两个最显著的不同如下:
urllib2can accept a
Requestobject
to set the headers for a URL request,
urllibaccepts only a URL. That means, you cannot masquerade your User Agent string etc.
urllib2可以接受一个Request类的实例来设置URL请求的headers,urllib仅可以接受URL。这意味着,你不可以伪装你的User Agent字符串等。
[align=justify][/align]
urllibprovides the
urlencodemethod
which is used for the generation of GET query strings,
urllib2doesn't have such a function. This is one of the reasons
why
urllibis often used along with
urllib2.
urllib提供urlencode方法用来GET查询字符串的产生,而urllib2没有。这是为何urllib常和urllib2一起使用的原因。
[align=justify][/align]
[align=justify][/align]
For other differences between
urlliband
urllib2refer
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://www.hacksparrow.com/python-difference-between-urllib-and-urllib2.html http://www.hacksparrow.com/python-difference-between-urllib-and-urllib2.html
相关文章推荐
- Python基础教程之正则表达式基本语法以及re模块
- python安装MySQLdb模块-(ubuntu,windows)
- python函数里面的装饰器和闭包的理解
- Python中的sorted函数以及operator.itemgetter函数
- python2.7:IOError: [Errno 2] No such file or directory:
- python 如何重载模块,用一个简单的例子来解释
- PHP + PYTHON 多任务多线程,后台运行,计划任务-实现方法
- Python信号处理模块signal
- python中字典(dict)常见用法
- python2.7:模块安装4种方法
- Python基础教程
- python写个图片爬虫
- python中元组(tuple)用法总结
- python
- 转载一篇python类的博文
- Python入门 之 dict
- Python-pip 安装失败问题解决
- 初始Python类
- Python基础教程之正则表达式基本语法以及re模块
- 用Python实现斐波那契(Fibonacci)函数