Python接口自动化
发布时间:2020-12-20 10:55:00 所属栏目:Python 来源:网络整理
导读:requests库 requests库介绍: Requests是一个优雅而简单的Python HTTP库,专为人类而构建。(来自官方的介绍) requests 是一个很实用的Python?HTTP客户端库,编写爬虫和测试服务器响应数据时经常会用到,很适合做接口测试。 request库使用 requests库安装:
requests库requests库介绍:Requests是一个优雅而简单的Python HTTP库,专为人类而构建。(来自官方的介绍)
request库使用requests库安装: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 string 16 defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers 17 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 data 20 before giving up,as a float,or a :ref:`(connect timeout,read 21 timeout) <timeouts>` tuple. 22 :type timeout: float or tuple 23 :param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``. 24 :type allow_redirects: bool 25 :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 verify 27 the server‘s TLS certificate,or a string,in which case it must be a path 28 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>` object 32 :rtype: requests.Response 33 34 Usage:: 35 36 >>> import requests 37 >>> 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 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) ?其中,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)‘‘‘ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- double equals vs在python中
- python – 在大熊猫DataFrame中选择和删除具有相同名称的列
- python – Django AttributeError:’InterestsForm’对象没
- python – Sublime Text:显示当前外部元素名称
- python – Mako模板:如何找到包含当前模板的模板的名称?
- python – 如何在生成的生成脚本前添加路径
- Python 扫描IP段 指定端口是否开放
- 强力Django+杀手级xadmin开发在线教育网站 采用 Python3.7全
- Python设计模式编程中的备忘录模式与对象池模式示例
- python – GAE上的Twitter流媒体