在Python中涉及到URL请求相关的操作涉及到模块有urllib,urllib2,requests,其中urllib和urllib2是Python自带的HTTP访问标准库,requsets是第三方库,需要自行安装。requests是第三方库,可以想到在使用起来它可能是最方便的一个。

urllib和urllib2

      urllib和urllib2模块都是跟url请求相关的,但是提供的功能是不同的。我们常用的urllib2的请求方式:

response = urllib2.urlopen('http://www.baidu.com')

在参数中可以传入url和request对象,传入request可以来设置URL请求的headers,可以伪装成浏览器(当请求的网站进行请求监测的时候),urllib是只能传入url的,这也是二者的差别之一:

user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
request = urllib2.Request(url, headers={
'User-Agent': user_agent
})
response = urllib2.urlopen(request)

但是在urllib中一些方法是没有加入的urllib2当中的,在有些时候也是需要urllib的辅助,这些我还暂时不是很懂,等遇到的时候再深究下,例如涉及到unicode编码相关的是只能用urllib来处理的,这也是二者的差别之一。

在urllib2中openurl函数还有几个常用的参数:data、timeout,阻塞操作以秒为单位,data和request对象在Request类中说明。

Requset类有5个参数:url,data,headers,origin_req_host,unverifiable 。

  1. url不必说了就是我们要请求的url地址
  2. data是我们要向服务器提交的额外的数据,如果没有数据可以为None,请求如果是由数据的话那就是POST请求,这些数据需要以标准的格式编码然后传送给request对象。
  3. headers请求头,是一个字典类型的。它是告诉服务器请求的一些信息,例如像请求的浏览器信息,操作系统信息,cookie,返回信息格式,缓存,是否支持压缩等等,像一些反爬虫的网站会监测请求的类型,我们需要伪装成浏览器而不是直接发起请求,例如上面代码里的User-Agent
  4. origin_req_host是RFC2965定义的源交互的request-host。默认的取值是cookielib.request_host(self)。这是由用户发起的原始请求的主机名或IP地址。例如,如果请求的是一个HTML文档中的图像,这应该是包含该图像的页面请求的request-host。
  5. unverifiable代表请求是否是无法验证的,它也是由RFC2965定义的。默认值为false。一个无法验证的请求是,其用户的URL没有足够的权限来被接受。

我们在请求的时候不一定每次都是请求的成功的页面,如果请求url不正常报错也是需要做好判断处理的。

try:
response = urllib2.urlopen('http://www.baidu.com')
except urllib2.HTTPError as e:
print e.code
print e.reason
except urllib2.URLError as e:
print e.reason
else:
response.read()

当发生错误抛出异常我们可以捕获查看异常原因,获取请求的状态码。getcode()方法也可以获取请求状态码,附录:

# Table mapping response codes to messages; entries have the
# form {code: (shortmessage, longmessage)}.
responses = {
100: ('Continue', 'Request received, please continue'),
101: ('Switching Protocols',
'Switching to new protocol; obey Upgrade header'), 200: ('OK', 'Request fulfilled, document follows'),
201: ('Created', 'Document created, URL follows'),
202: ('Accepted',
'Request accepted, processing continues off-line'),
203: ('Non-Authoritative Information', 'Request fulfilled from cache'),
204: ('No Content', 'Request fulfilled, nothing follows'),
205: ('Reset Content', 'Clear input form for further input.'),
206: ('Partial Content', 'Partial content follows.'), 300: ('Multiple Choices',
'Object has several resources -- see URI list'),
301: ('Moved Permanently', 'Object moved permanently -- see URI list'),
302: ('Found', 'Object moved temporarily -- see URI list'),
303: ('See Other', 'Object moved -- see Method and URL list'),
304: ('Not Modified',
'Document has not changed since given time'),
305: ('Use Proxy',
'You must use proxy specified in Location to access this '
'resource.'),
307: ('Temporary Redirect',
'Object moved temporarily -- see URI list'), 400: ('Bad Request',
'Bad request syntax or unsupported method'),
401: ('Unauthorized',
'No permission -- see authorization schemes'),
402: ('Payment Required',
'No payment -- see charging schemes'),
403: ('Forbidden',
'Request forbidden -- authorization will not help'),
404: ('Not Found', 'Nothing matches the given URI'),
405: ('Method Not Allowed',
'Specified method is invalid for this server.'),
406: ('Not Acceptable', 'URI not available in preferred format.'),
407: ('Proxy Authentication Required', 'You must authenticate with '
'this proxy before proceeding.'),
408: ('Request Timeout', 'Request timed out; try again later.'),
409: ('Conflict', 'Request conflict.'),
410: ('Gone',
'URI no longer exists and has been permanently removed.'),
411: ('Length Required', 'Client must specify Content-Length.'),
412: ('Precondition Failed', 'Precondition in headers is false.'),
413: ('Request Entity Too Large', 'Entity is too large.'),
414: ('Request-URI Too Long', 'URI is too long.'),
415: ('Unsupported Media Type', 'Entity body in unsupported format.'),
416: ('Requested Range Not Satisfiable',
'Cannot satisfy request range.'),
417: ('Expectation Failed',
'Expect condition could not be satisfied.'), 500: ('Internal Server Error', 'Server got itself in trouble'),
501: ('Not Implemented',
'Server does not support this operation'),
502: ('Bad Gateway', 'Invalid responses from another server/proxy.'),
503: ('Service Unavailable',
'The server cannot process the request due to a high load'),
504: ('Gateway Timeout',
'The gateway server did not receive a timely response'),
505: ('HTTP Version Not Supported', 'Cannot fulfill request.'),
}

Requests

requests使用的是urllib3,继承了urllib2的所有特性,Requests支持HTTP连接保持和连接池,支持使用cookie保持会话,支持文件上传,支持自 动确定响应内容的编码,支持国际化的 URL 和 POST 数据自动编码。

get请求

response = requests.get('http://www.baidu.com')
print response.text

post请求

response = requests.post('http://api.baidu.com', data={
'data': 'value'
})

定制headers

response = requests.get('http://www.baidu.com', headers={
'User-Agent': user_agent
})

response返回数据的相关操作:

r.status_code #响应状态码
    r.raw #返回原始响应体,也就是 urllib 的 response 对象,使用 r.raw.read() 读取
    r.content #字节方式的响应体,会自动为你解码 gzip 和 deflate 压缩
    r.text #字符串方式的响应体,会自动根据响应头部的字符编码进行解码
    r.headers #以字典对象存储服务器响应头,但是这个字典比较特殊,字典键不区分大小写,若键不存在则返回None
特殊方法:
    r.json() #Requests中内置的JSON解码器
    r.raise_for_status() #失败请求(非200响应)抛出异常

----------------未完待续---------------

04-21 00:42