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:

  • 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 header ,urllib不能
  • 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功能。
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

  1. What is User Agent?
  2. What is the difference between GET and POST request methods?
  3. What is HTTP?

References

    1. urllib
    2. urllib2

python urllib和urllib2 区别的更多相关文章

  1. Python:urllib和urllib2的区别(转)

    原文链接:http://www.cnblogs.com/yuxc/ 作为一个Python菜鸟,之前一直懵懂于urllib和urllib2,以为2是1的升级版.今天看到老外写的一篇<Python: ...

  2. Python:urllib和urllib2的区别

    urllib和urllib2都是处理url请求的两个模块,但是相互之间存在不同,不能相互取代 urllib2可以接受一个Reuqest类的实例来设置URL请求的headers,urllib仅可以接受U ...

  3. Python urllib和urllib2模块学习(二)

    一.urllib其它函数 前面介绍了 urllib 模块,以及它常用的 urlopen() 和 urlretrieve()函数的使用介绍.当然 urllib 还有一些其它很有用的辅助方法,比如对 ur ...

  4. Python urllib和urllib2模块学习(一)

    (参考资料:现代魔法学院 http://www.nowamagic.net/academy/detail/1302803) Python标准库中有许多实用的工具类,但是在具体使用时,标准库文档上对使用 ...

  5. python urllib 和 urllib2

    urllib 和 urllib2 都是接受URL请求的相关模块,但是提供了不同的功能.两个最显著的不同如下: urllib 仅可以接受URL,不能创建 设置了headers 的Request 类实例: ...

  6. Python urllib和urllib2模块学习(三)

    build_opener()详解: 1.urllib2.urlopen()函数不支持验证.cookie或者其它HTTP高级功能,要支持这些功能,必须使用build_opener()函数创建自定这句话的 ...

  7. urllib与urllib2的学习总结(python2.7.X): python urllib与urllib2

    https://www.cnblogs.com/wly923/archive/2013/05/07/3057122.html

  8. 爬虫新手学习2-爬虫进阶(urllib和urllib2 的区别、url转码、爬虫GET提交实例、批量爬取贴吧数据、fidder软件安装、有道翻译POST实例、豆瓣ajax数据获取)

    1.urllib和urllib2区别实例 urllib和urllib2都是接受URL请求相关模块,但是提供了不同的功能,两个最显著的不同如下: urllib可以接受URL,不能创建设置headers的 ...

  9. 理解urllib、urllib2及requests区别及运用

    urllib and urllib2 区别 –博主提示:下面的是python2中的用法,python3需要做出相应修改. urllib和urllib2模块都做与请求URL相关的操作,但他们提供不同的功 ...

随机推荐

  1. UVA-Matrix Chain Multiplication(栈)

     Matrix Chain Multiplication  Suppose you have to evaluate an expression like A*B*C*D*E where A,B,C, ...

  2. BZOJ 1485: [HNOI2009]有趣的数列( catalan数 )

    打个表找一下规律可以发现...就是卡特兰数...卡特兰数可以用组合数计算.对于这道题,ans(n) = C(n, 2n) / (n+1) , 分解质因数去算就可以了... -------------- ...

  3. Java ByteArrayOutputStream中buf 的大小增长问题

    问题:写入固定长度的字符串[write(byte b[])],观察ByteArrayOutputStream中buf 的大小始终比字符串 Bytes的Size大很多,很是不解 分析发现: privat ...

  4. 关于jq操作table下多个type=radio的input的选中

    假如有2个table: <table id="table1" border="0"> <tr> <td><input ...

  5. Python每日一练(1):计算文件夹内各个文章中出现次数最多的单词

    #coding:utf-8 import os,re path = 'test' files = os.listdir(path) def count_word(words): dic = {} ma ...

  6. HDU 1001 Sum Problem

    /* 注意可以是负整数,而且在过程中会超过int,所以要用longlong */ #include <cstdio> int main() { long long n; while (sc ...

  7. maven 添加自己的包

    mvn install:install-file -Dfile=d:/flea.jar -DgroupId=com.flea.bussiness -DartifactId=flea -Dversion ...

  8. Jsp、Servlet

    1 forward.redirect forward 转发是服务器行为,浏览器根本不知道服务器发送的内容是从哪儿来,所以它的地址栏中还是原来的地址. redirect 重定向是客户端行为.redire ...

  9. JS动态增加页面上的控件实例

    <input   type="button"   value="继续添加"   onclick="append();"/ > & ...

  10. java--String常量池问题的几个例子

    关于string内存分配不错的博客:http://blog.csdn.net/rj042/article/details/6871030 String常量池问题的几个例子 示例1: Java代码 St ...