python中requests的应用

方式一

response = requests.request(method, url, **kwargs)

r = requests.request('GET', url, **kwargs)
r = requests.request('HEAD', url, **kwargs)
r = requests.request('POST', url, **kwargs)
r = requests.request('PUT', url, **kwargs)
r = requests.request('PATCH', url, **kwargs)
r = requests.request('delete', url, **kwargs)
r = requests.request('OPTIONS', url, **kwargs)

方式二

 response = requests.get(url,**kwargs)

requests.get(url,**kwargs)     获取HTML网页的主要方法,对应于HTTP的GET
requests.head(url,**kwargs)    获取HTML网页头信息的方法,对应于HTTP的HEAD
requests.post(url,**kwargs)    向HTML网页提交POST请求的方法,对应于HTTP的POST
requests.put(url,**kwargs)     向HTML网页提交PUT请求的方法,对应于HTTP的PUT
requests.patch(url,**kwargs)   向HTML网页提交局部修改请求,对应于HTTP的PATCH
requests.delete(url,**kwargs)  向HTML页面提交删除请求,对应于HTTP的DELETE

∙ **kwargs: 控制访问的参数,均为可选项

params :字典或字节序列,作为参数增加到url中

kv = {'key1': 'value1', 'key2': 'value2'}
r = requests.request('GET', 'http://python123.io/ws', params=kv)
print(r.url)

data :字典、字节序列或文件对象,作为Request的内容

kv = {'key1': 'value1', 'key2': 'value2'}
r = requests.request('POST', 'http://python123.io/ws', data=kv)
body = '主体内容'
r = requests.request('POST', 'http://python123.io/ws', data=body)

json:: JSON格式的数据,作为Request的内容

kv = {'key1': 'value1'}
r = requests.request('POST', 'http://python123.io/ws', json=kv)

headers:字典,HTTP定制头

hd = {'user‐agent': 'Chrome/10'}
r = requests.request('POST', 'http://python123.io/ws', headers=hd)

cookies :字典或CookieJar,Request中的cookie

auth:元组,支持HTTP认证功能

许多要求身份认证的web服务都接受 HTTP Basic Auth。这是最简单的一种身份认证,并且 Requests 对这种认证方式的支持是直接开箱即可用。

以 HTTP Basic Auth 发送请求非常简单:

>>> from requests.auth import HTTPBasicAuth
>>> requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))
<Response [200]>
事实上,HTTP Basic Auth 如此常见,Requests 就提供了一种简写的使用方式:

>>> requests.get('https://api.github.com/user', auth=('user', 'pass'))
<Response [200]>
像这样在一个元组中提供认证信息与前一个 HTTPBasicAuth 例子是完全相同的。

files:字典类型,传输文件

fs = {'file': open('data.xls', 'rb')}
r = requests.request('POST', 'http://python123.io/ws', files=fs)

timeout :: 设定超时时间,秒为单位

r = requests.request('GET', 'http://www.baidu.com', timeout=10)

proxies :字典类型,设定访问代理服务器,可以增加登录认证

pxs = { 'http': 'http://user:[email protected]:1234'
'https': 'https://10.10.10.1:4321' }
r = requests.request('GET', 'http://www.baidu.com', proxies=pxs)

allow_redirects :True/False,默认为True,重定向开关

stream:True/False,默认为True,获取内容立即下载开关

tarball_url = 'https://github.com/kennethreitz/requests/tarball/master'
r = requests.get(tarball_url, stream=True)

verify :True/False,默认为True,认证SSL证书开关

Requests 可以为 HTTPS 请求验证 SSL 证书,就像 web 浏览器一样。SSL 验证默认是开启的,如果证书验证失败,Requests 会抛出 SSLError:

>>> requests.get('https://requestb.in')
requests.exceptions.SSLError: hostname 'requestb.in' doesn't match either of '*.herokuapp.com', 'herokuapp.com'
在该域名上我没有设置 SSL,所以失败了。但 Github 设置了 SSL:

>>> requests.get('https://github.com', verify=True)
<Response [200]>
你可以为 verify 传入 CA_BUNDLE 文件的路径,或者包含可信任 CA 证书文件的文件夹路径:

>>> requests.get('https://github.com', verify='/path/to/certfile')
或者将其保持在会话中:

s = requests.Session()
s.verify = '/path/to/certfile'
注解
如果 verify 设为文件夹路径,文件夹必须通过 OpenSSL 提供的 c_rehash 工具处理。

你还可以通过 REQUESTS_CA_BUNDLE 环境变量定义可信任 CA 列表。

如果你将 verify 设置为 False,Requests 也能忽略对 SSL 证书的验证。

>>> requests.get('https://kennethreitz.org', verify=False)
<Response [200]>
默认情况下, verify 是设置为 True 的。选项 verify 仅应用于主机证书。

# 对于私有证书,你也可以传递一个 CA_BUNDLE 文件的路径给 verify。你也可以设置 # REQUEST_CA_BUNDLE 环境变量。

cert:本地SSL证书路径

你也可以指定一个本地证书用作客户端证书,可以是单个文件(包含密钥和证书)或一个包含两个文件路径的元组:

>>> requests.get('https://kennethreitz.org', cert=('/path/client.cert', '/path/client.key'))
<Response [200]>
或者保持在会话中:

s = requests.Session()
s.cert = '/path/client.cert'
如果你指定了一个错误路径或一个无效的证书:

>>> requests.get('https://kennethreitz.org', cert='/wrong_path/client.pem')
SSLError: [Errno 336265225] _ssl.c:347: error:140B0009:SSL routines:SSL_CTX_use_PrivateKey_file:PEM lib


 

猜你喜欢

转载自blog.csdn.net/qq_15256443/article/details/81390077