python_urllib urllib2

作为一个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,比其他几个模块好用。

时间: 2024-08-03 15:37:10

python_urllib urllib2的相关文章

Python——深入理解urllib、urllib2及requests(requests不建议使用?)

深入理解urllib.urllib2及requests            python Python 是一种面向对象.解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年,Python 源代码同样遵循 GPL(GNU General Public License)协议[1] .Python语法简洁而清晰,具有丰富和强大的类库. urllib and urllib2 区别 urllib和urllib2模块都做与请求URL相关的操作,但

python之urllib2简单解析HTML页面

一.urllib2简单获取html页面 #!/usr/bin/env python # -*- coding:utf-8 -*- import urllib2 response = urllib2.urlopen('http://www.baidu.com'); html = response.read(); print html 简单的几行代码就能拿到html页面,接下来局势html的解析工作了. 想象很美好,实际操作就出问题了.baidu没有禁止机器人抓取可以正常抓取到页面,但是比如:htt

爬虫之urllib2库的基本使用

urllib2库的基本使用 所谓网页抓取,就是把URL地址中指定的网络资源从网络流中读取出来,保存到本地. 在Python中有很多库可以用来抓取网页,我们先学习urllib2. urllib2 是 Python2.7 自带的模块(不需要下载,导入即可使用) urllib2 官方文档:https://docs.python.org/2/library/urllib2.html urllib2 源码:https://hg.python.org/cpython/file/2.7/Lib/urllib2

用urllib2实现简单的网络爬虫1

玩python的同学都避免不了想写个爬虫玩玩,并且一般都以抓取XX图片为主,当然楼主也不例外~~ 这里先用比较原始的方式:urllib2 + 正则表达式,后面再尝试requests 背景:假设有个网站,是关于一些艺术家及其作品介绍的,登陆进去后,每一页是艺术家的头像和名字列表(由于艺术家很多,所以有很多页):  点击艺术家的头像或名字,就进入该艺术家的主页,主页上有该艺术家的详细介绍和作品列表(由于每个艺术家有很多作品,所有可能会有很多页):   点击其中一部作品,就进入该作品的详细介绍页面,包

httplib,urllib和urllib2

一.httplib实现了HTTP和HTTPS的客户端协议,一般不直接使用,在python更高层的封装模块中(urllib,urllib2)使用了它的http实现. import httplib conn = httplib.HTTPConnection("google.com") conn.request('get', '/') print conn.getresponse().read() conn.close() httplib.HTTPConnection ( host [ , 

python中使用urllib2伪造HTTP报头的2个方法

在采集网页信息的时候,经常需要伪造报头来实现采集脚本的有效执行 下面,我们将使用urllib2的header部分伪造报头来实现采集信息 方法1. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #!/usr/bin/python # -*- coding: utf-8 -*- #encoding=utf-8 #Filename:urllib2-header.py   import ur

urllib2.HTTPError: HTTP Error 403: Forbidden

这个问题主要是没有headers,加入一些内容就可以了 示例: # -*- coding: UTF-8 -*- import urllib2 site= "http://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/getHistoricalData.jsp?symbol=JPASSOCIAT&fromDate=1-JAN-2012&toDate=1-AUG-2012&datePeriod=un

Python urllib2 模块学习笔记

2015.3.6  urllib2的使用方法大致如下 # 定制Handler处理函数 opener = urllib2.build_opener(ProxyHandler, HTTPHandler) urllib2.install_opener(opener) # 定制URL参数 request = urllib2.Request() request.add_headers(xxx) # 打开URL,返回file-like对象 response = urllib2.urlopen(req) #

Python标准库 urllib2 的使用

1.Proxy 的设置 urllib2 默认会使用环境变量 http_proxy 来设置 HTTP Proxy. 如果想在程序中明确控制 Proxy,而不受环境变量的影响,可以使用下面的方式 import urllib2 enable_proxy = True proxy_handler = urllib2.ProxyHandler({"http" : 'http://some-proxy.com:8080'}) null_proxy_handler = urllib2.ProxyHa