requests库
requests库介绍:
Requests是一个优雅而简单的Python HTTP库,专为人类而构建。(来自官方的介绍)
requests是一个很实用的Python HTTP客户端库,编写爬虫和测试服务器响应数据时经常会用到,对测试来说做接口测试会很方便。
request库使用
requests库安装:
requests是第三方的库,需要先安装 :pip3 install requests(python3版本)
requests源码
1 def request(method, url, **kwargs):2 """Constructs and sends a :class:`Request `.3
4 :param method: method for the new :class:`Request` object.5 :param url: URL for the new :class:`Request` object.6 :param params: (optional) Dictionary, list of tuples or bytes to send7 in the query string for the :class:`Request`.8 :param data: (optional) Dictionary, list of tuples, bytes, or file-like9 object to send in the body of the :class:`Request`.10 :param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`.11 :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.12 :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.13 :param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``) for multipart encoding upload.14 ``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')``15 or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string16 defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers17 to add for the file.18 :param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.19 :param timeout: (optional) How many seconds to wait for the server to send data20 before giving up, as a float, or a :ref:`(connect timeout, read21 timeout) ` tuple.22 :type timeout: float or tuple23 :param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``.24 :type allow_redirects: bool25 :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.26 :param verify: (optional) Either a boolean, in which case it controls whether we verify27 the server's TLS certificate, or a string, in which case it must be a path28 to a CA bundle to use. Defaults to ``True``.29 :param stream: (optional) if ``False``, the response content will be immediately downloaded.30 :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.31 :return: :class:`Response ` object32 :rtype: requests.Response33
34 Usage::35
36 >>> import requests37 >>> req = requests.request('GET', 'https://httpbin.org/get')38 39 """
40
41 #By using the 'with' statement we are sure the session is closed, thus we
42 #avoid leaving sockets open which can trigger a ResourceWarning in some
43 #cases, and look like a memory leak in others.
44 with sessions.Session() as session:45 return session.request(method=method, url=url, **kwargs)
View Code
其中,month主要是用get和post
requests模块发送请求有data、json、params三种携带参数的方法。
params在get请求中使用,data、json在post请求中使用。
'''
requests--get方法
r=requests.get('https://movie.douban.com/')
print(r.status_code) #查看状态码
print(r.text) #查看响应内容
print(r.content.decode('utf-8')) #查看字节流(二进制)
print(r.url) #查看请求地址
print(r.encoding) #查看字符编码
print(r.cookies) #查看cookie
print(r.headers) #查看请求头信息
# 当返回的响应格式是json时,可以用r.json())
# 这里有两种方式,一种是直接用请求地址,另一种是用参数的方式传递parames
url='https://movie.douban.com/subject_search?search_text=%E5%93%AA%E5%90%92&cat=1002'
url1='https://movie.douban.com/subject_search?search_text=哪吒&cat=1002'
paydata={'search_text':'哪吒','cat':1002}
r=requests.get(url1,params=paydata)
'''
post方法:
def post(url, data=None, json=None, **kwargs):
r"""Sends a POST request.
:param url: URL for the new :class:`Request` object.
:param data: (optional) Dictionary, list of tuples, bytes, or file-like
object to send in the body of the :class:`Request`.
:param json: (optional) json data to send in the body of the :class:`Request`.
:param \*\*kwargs: Optional arguments that ``request`` takes.
:return: :class:`Response ` object
:rtype: requests.Response"""
return request('post', url, data=data, json=json, **kwargs)
url='https://testyyfax8511.yylending.com/'login_heades={'Referer': 'https://testyyfax8511.yylending.com/web/login.html?_z=/web/user/index.html','user-agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36'}
login_data={'account':'18885110001','password':'204Ofz2Lrfyng'}
test_login=requests.post(url+'node_api/user.do?fn=login',headers=login_heades,data=login_data)print(test_login.text)print(json.dumps(test_login.json(),indent=True,ensure_ascii=False))
当请求参数为form表单是,用data;当请求参数为josn格式时,用json;
当响应格式是json时,用json查看会更合适(查看响应字段的时候,用json取值更方便)
request中超时情况的处理
#客户端发送到服务端的请求,当服务端响应的时间比较长是,可以加上等待时间
r=requests.get('http://www.baidu.com',timeout=6)
requests中安全证书的处理思路
当请求需要安全证书的时候,可以通过忽略安全证书的方式处理或者带上安全证书的路径,推荐用忽略的方式。
r=requests.get('http://www.baidu.com',verify=False)
requests中cookie的处理
第一种方法是直接在请求中带上具体的cookie的值;
第二种方法,可以写一个方法,返回登录后的cookie,然后在需要cookies的接口中带上
requests中对token的处理
处理方法同cookie是一样的思路
requests对session会话对象的处理??
session,保存了客户端与服务器之间的通信信息。能够跨请求保持某些参数,比如它会在同一个session实例发出的所有请求之间保持cookie
requests对文件上传的处理
requests对文件下载的处理