python Request模块
---恢复内容开始---
Request的五种请求方式
方法 |
说明 |
requests.request() |
构造一个请求,支撑以下各方法的基础方法 |
requests.get() |
获取HTML网页的主要方法,对应于HTTP的GET |
requests.head() |
获取HTML网页头信息的方法,对应于HTTP的HEAD |
request.post() |
向HTML网页提交POST请求的方法,对应于HTTP的POST |
request.put() |
向HTML网页提交PUT请求的方法,对应于HTTP的PUT |
request.patch() |
向HTML网页提交局部修改请求,对应于HTTP的PATCH |
request.delete() |
向HTML页面提交删除请求,对应于HTTP的DELETE |
一:request.get():
源码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 def get(url, params=None, **kwargs): 2 r"""Sends a GET request. 3 4 :param url: URL for the new :class:`Request` object. 5 :param params: (optional) Dictionary, list of tuples or bytes to send 6 in the body of the :class:`Request`. 7 :param \*\*kwargs: Optional arguments that ``request`` takes. 8 :return: :class:`Response <Response>` object 9 :rtype: requests.Response 10 """ 11 12 kwargs.setdefault('allow_redirects', True) 13 return request('get', url, params=params, **kwargs)
参数解析及使用
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import requests 2 r=requests.request('post','http://www.oldboyedu.com',params={'k1':'python'}) 3 print(r.url) 4 #https://www.oldboyedu.com/?k1=python
response对象的属性
属性 | 说明 |
---|---|
r.status_code | HTTP请求的返回状态,200表示连接成功,404表示失败 |
r.text | HTTP响应内容的字符串形式,即,url对应的页面内容 |
r.encoding | 从HTTP header中猜测的响应内容编码方式 |
r.apparent_encoding | 从内容分析出的响应内容编码方式(备选编码方式) |
r.content | HTTP响应内容的二进制形式 |
r.json | 返回json数据 |
r.request | 返回请求方式 |
r.headers | 返回请求头 |
r.cookies | 返回cookie |
理解Response的编码
属性 | 说明 |
---|---|
r.encoding | 从HTTP header中猜测的响应内容编码方式 |
r.apparent_encoding | 从内容中分析出的响应内容编码方式(备选编码方式) |
r.encoding:如果header中不存在charset,则认为编码为ISO-8859-1
r.apparent_encoding:根据网页内容分析出的编码方式。
理解Requests库的异常
异常 | 说明 |
---|---|
requests.ConnectionError | 网络连接错误异常,如DNS查询失败、拒绝连接等 |
requests.HTTPError | HTTP错误异常 |
requests.URLRequired | URL缺失异常 |
requests.TooMangRedirects | 超过最大重定向次数,产生重定向异常 |
requests.ConnectTimeout | 连接远程服务器超时异常 |
requests.Timeout | 请求URL超时,产生超时异常 |
r.raise_for_status() | 如果不是200,产生异常requests.HTTPError |
更多参数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 :param method: method for the new :class:`Request` object. 2 :param url: URL for the new :class:`Request` object. 3 :param params: (optional) Dictionary, list of tuples or bytes to send 4 in the body of the :class:`Request`. 5 :param data: (optional) Dictionary, list of tuples, bytes, or file-like 6 object to send in the body of the :class:`Request`. 7 :param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`. 8 :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`. 9 :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`. 10 :param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``) for multipart encoding upload. 11 ``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')`` 12 or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string 13 defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers 14 to add for the file. 15 :param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth. 16 :param timeout: (optional) How many seconds to wait for the server to send data 17 before giving up, as a float, or a :ref:`(connect timeout, read 18 timeout) <timeouts>` tuple. 19 :type timeout: float or tuple 20 :param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``. 21 :type allow_redirects: bool 22 :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy. 23 :param verify: (optional) Either a boolean, in which case it controls whether we verify 24 the server's TLS certificate, or a string, in which case it must be a path 25 to a CA bundle to use. Defaults to ``True``. 26 :param stream: (optional) if ``False``, the response content will be immediately downloaded. 27 :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.
参数运用
- params
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # - 可以是字典 2 # - 可以是字符串 3 # - 可以是字节(ascii编码以内) 4 5 # requests.request(method='get', 6 # url='http://127.0.0.1:8000/test/', 7 # params={'k1': 'v1', 'k2': '水电费'}) 8 9 # requests.request(method='get', 10 # url='http://127.0.0.1:8000/test/', 11 # params="k1=v1&k2=水电费&k3=v3&k3=vv3") 12 13 # requests.request(method='get', 14 # url='http://127.0.0.1:8000/test/', 15 # params=bytes("k1=v1&k2=k2&k3=v3&k3=vv3", encoding='utf8'))
- json
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # 将json中对应的数据进行序列化成一个字符串,json.dumps(...) 2 # 然后发送到服务器端的body中,并且Content-Type是 {'Content-Type': 'application/json'}
- headers
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # 发送请求头到服务器端 2 requests.request(method='POST', 3 url='http://127.0.0.1:8000/test/', 4 json={'k1': 'v1', 'k2': '水电费'}, 5 headers={'Content-Type': 'application/x-www-form-urlencoded'} 6 )
- cookies
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # 发送Cookie到服务器端 2 requests.request(method='POST', 3 url='http://127.0.0.1:8000/test/', 4 data={'k1': 'v1', 'k2': 'v2'}, 5 cookies={'cook1': 'value1'}, 6 )
- files
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # 发送文件 2 # file_dict = { 3 # 'f1': open('readme', 'rb') 4 # } 5 # requests.request(method='POST', 6 # url='http://127.0.0.1:8000/test/', 7 # files=file_dict) 8 9 # 发送文件,定制文件名 10 # file_dict = { 11 # 'f1': ('test.txt', open('readme', 'rb')) 12 # } 13 # requests.request(method='POST', 14 # url='http://127.0.0.1:8000/test/', 15 # files=file_dict) 16 17 # 发送文件,定制文件名 18 # file_dict = { 19 # 'f1': ('test.txt', "hahsfaksfa9kasdjflaksdjf") 20 # } 21 # requests.request(method='POST', 22 # url='http://127.0.0.1:8000/test/', 23 # files=file_dict) 24 25 # 发送文件,定制文件名 26 # file_dict = { 27 # 'f1': ('test.txt', "hahsfaksfa9kasdjflaksdjf", 'application/text', {'k1': '0'}) 28 # } 29 # requests.request(method='POST', 30 # url='http://127.0.0.1:8000/test/', 31 # files=file_dict)
- auth
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 from requests.auth import HTTPBasicAuth, HTTPDigestAuth 2 3 ret = requests.get('https://api.github.com/user', auth=HTTPBasicAuth('wupeiqi', 'sdfasdfasdf')) 4 print(ret.text) 5 6 # ret = requests.get('http://192.168.1.1', 7 # auth=HTTPBasicAuth('admin', 'admin')) 8 # ret.encoding = 'gbk' 9 # print(ret.text) 10 11 # ret = requests.get('http://httpbin.org/digest-auth/auth/user/pass', auth=HTTPDigestAuth('user', 'pass')) 12 # print(ret) 13 #
- timeout
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # ret = requests.get('http://google.com/', timeout=1) 2 # print(ret) 3 4 # ret = requests.get('http://google.com/', timeout=(5, 1)) 5 # print(ret) 6 pass 7 第一个时间为链接时间,第二个时间为服务器发送第一个数据的时间
- allow-redirects:是否允许重定向
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # ret = requests.get('http://google.com/', timeout=1) 2 # print(ret) 3 4 # ret = requests.get('http://google.com/', timeout=(5, 1)) 5 # print(ret) 6 pass
- proxies:设置代理
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # proxies = { 2 # "http": "61.172.249.96:80", 3 # "https": "http://61.185.219.126:3128", 4 # } 5 6 # proxies = {'http://10.20.1.128': 'http://10.10.1.10:5323'} 7 8 # ret = requests.get("http://www.proxy360.cn/Proxy", proxies=proxies) 9 # print(ret.headers) 10 11 12 # from requests.auth import HTTPProxyAuth 13 # 14 # proxyDict = { 15 # 'http': '77.75.105.165', 16 # 'https': '77.75.105.165' 17 # } 18 # auth = HTTPProxyAuth('username', 'mypassword') 19 # 20 # r = requests.get("http://www.google.com", proxies=proxyDict, auth=auth) 21 # print(r.text)
- stream:相当于下一点保存一点
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 ret = requests.get('http://127.0.0.1:8000/test/', stream=True) 2 print(ret.content) 3 ret.close() 4 5 # from contextlib import closing 6 # with closing(requests.get('http://httpbin.org/get', stream=True)) as r: 7 # # 在此处理响应。 8 # for i in r.iter_content(): 9 # print(i)
- verify:指定是否进行https认证
requests.get('https://kennethreitz.org',verify=False)
- cert是保存本地ssl证书路径的字段
requests.get('https://kennethreitz.org',cert=('/path/client.cert','/path/client.key'))
其中data参数中的数据型类型只能是字符串,列表,数字,文件,不可以再数据里提交字典
json参数中的数据类型可以试字符串,列表,数字,还有字典,可序列化对象
requests库的session对象能够帮我们跨请求保持某些参数,也会在同一个session实例发出的所有请求之间保持cookies
参考:
链接:https://www.jianshu.com/p/d78982126318