Requests是一个优雅而简单的Python http库,专为人类而构建。(来自官方的介绍)
requests
是一个很实用的Python http客户端库,编写爬虫和测试服务器响应数据时经常会用到,很适合做接口测试。
requests是第三方的库,需要先安装 :pip3 install requests(python3版本)
requests源码1 def request(method,url,**kwargs): 2 """Constructs and sends a :class:`Request <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 send 7 in the query string for the :class:`Request`. 8 :param data: (optional) Dictionary,List of tuples,bytes,or file-like 9 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‘,‘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) <timeouts>` 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 <Response>` object32 :rtype: requests.Response33 34 Usage::35 36 >>> import requests37 >>> req = requests.request(‘GET‘,‘https://httpbin.org/get‘)38 <Response [200]>39 """40 41 # By using the ‘with‘ statement we are sure the session is closed,thus we42 # avoID leaving sockets open which can trigger a ResourceWarning in some43 # 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) #查看cookieprint(r.headers) #查看请求头信息# 当返回的响应格式是Json时,可以用r.Json())# 这里有两种方式,一种是直接用请求地址,另一种是用参数的方式传递paramesurl=‘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‘payload={‘search_text‘:‘哪吒‘,‘cat‘:1002}r=requests.get(url1,params=payload)‘‘‘总结
以上是内存溢出为你收集整理的Python接口自动化全部内容,希望文章能够帮你解决Python接口自动化所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)