龙空技术网

python 模块requests 发送 HTTP 请求

局外人LZ 55

前言:

如今看官们对“pythonhttphead”大体比较注重,咱们都需要知道一些“pythonhttphead”的相关知识。那么小编同时在网摘上搜集了一些关于“pythonhttphead””的相关资讯,希望你们能喜欢,我们快快来了解一下吧!

一、简介

requests 模块是 python 基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库。它比 urllib 更加方便,可以节约我们大量的工作

二、安装

pip install requestsimport requests

三、方法

requsts.requst(method, url,headers,cookies,proxies,timeout)method:请求方式;字符串类型url:请求的地址;字符串类型params:查询参数,get请求会自动对该参数编码,然后拼接到url;字典类型headers:请求头;字典类型cookies:浏览器cookie;字典类型proxies:代理服务ip;字典类型timeout:超时时间;整型requsts.get(url,params,headers,cookies,proxies,timeout):HTTP中的GET方法requsts.post(url,data,json,headers,cookies):HTTP中的POST方法json:参数为要发送到指定 url 的 JSON 对象data:要发送的数据,可以是字符串、字典或字节流。它用于在请求体中传递表单数据或其他非文件类型的数据auth:用于 HTTP 认证的用户名和密码,可以是元组形式的 (username, password)verify:是否验证服务器的 SSL 证书,默认为 True。可以设置为 False 禁用验证,或传递一个证书路径进行验证stream:是否使用流式传输,默认为 False。如果设置为 True,则响应内容不会立即下载,而是以流的形式逐步获取files:要上传的文件,可以是字典类型,包含文件字段名和文件路径requsts.put(url,headers,cookies,proxies,timeout):HTTP中的PUT方法requsts.patch(url,headers,cookies,proxies,timeout):HTTP中的PATCH方法requsts.delete(url,headers,cookies,proxies,timeout):HTTP中的DELETE方法requsts.head(ur):HTTP中的HEAD方法requests.session():会话对象让你能够跨请求保持某些参数

session = requests.Session() session.headers.update(headers)session.cookies.update(cookies)session.get()

三、返回结果信息reponse

reponse_result.status_code:http请求的返回状态,若为200则表示请求成功。reponse_result.raise_for_status():判断resp.status_code是否等于200,如果不等于,则抛出异常reponse_result.text:http响应内容的字符串形式,即返回的页面内容reponse_result.encoding:响应内容编码方式reponse_result.apparent_encoding:响应内容编码方式,备选编码方式reponse_result.content:http响应内容的二进制形式reponse_result.json():获取 json 格式的数据reponse_result.headers :获取响应头reponse_result.reason:响应状态的描述,比如 "Not Found" 或 "OK"reponse_result.close():关闭与服务器的连接reponse_result.cookies:返回一个cookie对象reponse_result.history:返回包含请求历史的响应对象列表reponse_result.links:返回响应的解析头链接reponse_result.ok:检查 "status_code" 的值,如果小于400,则返回 True,如果不小于 400,则返回 Falsereponse_result.raise_for_status():检查响应的状态码,如果状态码不是 200,则抛出异常。

标签: #pythonhttphead #python 调用http #nethttpspost请求 #python3发送post请求