python urllib和urllib2 区别
python有一个基础的库叫httplib。httplib实现了HTTP和HTTPS的客户端协议,一般不直接使用,在python更高层的封装模块中(urllib,urllib2)使用了它的http实现。
一直以为urllib2是urllib2的升级版,其实不是。
一篇老外写的文章:
What is the difference between urllib and urllib2 modules of Python?
You might be intrigued 好奇的by the existence of two separate URL modules in Python - urllib andurllib2. 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?
urllib and urllib2 are both Python modules that do URL request related stuff but offer different functionalities. Their two most significant differences are listed below:
urllib2can accept aRequestobject to set the headers for a URL request,urllibaccepts only a URL. That means, you cannot masquerade your User Agent string etc. urllib2能设置request header ,urllib不能urllibprovides theurlencodemethod which is used for the generation of GET query strings,urllib2doesn't have such a function. This is one of the reasons whyurllibis often used along withurllib2.
urllib提供urlencode功能。
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.
Exercise
- What is User Agent?
- What is the difference between GET and POST request methods?
- What is HTTP?
References
python urllib和urllib2 区别的更多相关文章
- Python:urllib和urllib2的区别(转)
原文链接:http://www.cnblogs.com/yuxc/ 作为一个Python菜鸟,之前一直懵懂于urllib和urllib2,以为2是1的升级版.今天看到老外写的一篇<Python: ...
- Python:urllib和urllib2的区别
urllib和urllib2都是处理url请求的两个模块,但是相互之间存在不同,不能相互取代 urllib2可以接受一个Reuqest类的实例来设置URL请求的headers,urllib仅可以接受U ...
- Python urllib和urllib2模块学习(二)
一.urllib其它函数 前面介绍了 urllib 模块,以及它常用的 urlopen() 和 urlretrieve()函数的使用介绍.当然 urllib 还有一些其它很有用的辅助方法,比如对 ur ...
- Python urllib和urllib2模块学习(一)
(参考资料:现代魔法学院 http://www.nowamagic.net/academy/detail/1302803) Python标准库中有许多实用的工具类,但是在具体使用时,标准库文档上对使用 ...
- python urllib 和 urllib2
urllib 和 urllib2 都是接受URL请求的相关模块,但是提供了不同的功能.两个最显著的不同如下: urllib 仅可以接受URL,不能创建 设置了headers 的Request 类实例: ...
- Python urllib和urllib2模块学习(三)
build_opener()详解: 1.urllib2.urlopen()函数不支持验证.cookie或者其它HTTP高级功能,要支持这些功能,必须使用build_opener()函数创建自定这句话的 ...
- urllib与urllib2的学习总结(python2.7.X): python urllib与urllib2
https://www.cnblogs.com/wly923/archive/2013/05/07/3057122.html
- 爬虫新手学习2-爬虫进阶(urllib和urllib2 的区别、url转码、爬虫GET提交实例、批量爬取贴吧数据、fidder软件安装、有道翻译POST实例、豆瓣ajax数据获取)
1.urllib和urllib2区别实例 urllib和urllib2都是接受URL请求相关模块,但是提供了不同的功能,两个最显著的不同如下: urllib可以接受URL,不能创建设置headers的 ...
- 理解urllib、urllib2及requests区别及运用
urllib and urllib2 区别 –博主提示:下面的是python2中的用法,python3需要做出相应修改. urllib和urllib2模块都做与请求URL相关的操作,但他们提供不同的功 ...
随机推荐
- Android发展简单介绍
Android一词的本义指“机器人”,同一时候也是Google于2007年11月5日宣布的基于Linux平台的开源手机操作系统的名称,该平台由操作系统.中间件.用户界面和应用软件组成,号称是首个为移动 ...
- 一个简单的文本编辑器。(是在DEV C++下写的)
//头文件// main.h #define CM_FILE_SAVEAS 9072 #define CM_FILE_EXIT 9071 #define CM_FILE_OPEN 9070 #defi ...
- 2.词法结构-JavaScript权威指南笔记
今天是第二章.所谓词法结构(lexical structure),就是写代码中最基本的东西,变量命名,注释,语句分隔等,这是抄书抄的... 1.字符集,必须是Unicode,反正Unicode是ASC ...
- BAE初试
BAE是百度的应用开发托管平台. 支持python nodejs java php 这几个环境~ 我在BAE上面搭建了1个wordpress. 记录下开启一个app的过程. 下面是所需工具 ---版 ...
- [Swust OJ 582]--放学了,抢机子了(SPFA)
题目链接:http://acm.swust.edu.cn/problem/0582/ Time limit(ms): 5000 Memory limit(kb): 65535 Descriptio ...
- HDU3535-AreYouBusy
描述: As having become a junior, xiaoA recognizes that there is not much time for her to AC problems, ...
- [置顶] 两主机搭建MySQL主从复制后,show slave status显示:Last_IO_Error: error connecting to master ……
两台主机A.B搭建mysql主从复制关系(A为master,B为slave)后,在slave上执行show slave status,结果中显示Last_IO_Error: error connect ...
- hdu 2874Connections between cities LCA
题目链接 给n个城市, m条边, q个询问, 每个询问, 输出城市a和b的最短距离, 如果不联通, 输出not connected. 用并查集判联通, 如果不连通, 那么两个联通块之间加一条权值很大的 ...
- A Byte of Python 笔记(8)
第10章 解决问题——编写一个 python 脚本 程序功能:为所有重要文件创建备份 设计: 1.需要备份的文件和目录由一个列表指定 2.备份应该保存在主备份目录中 3.文件备份称一个 zip 文件 ...
- JBoss Jopr
http://rhq.jboss.org/ https://issues.jboss.org/browse/JBPAPP6-947 挺好的网站: http://outofmemory.cn/code- ...