一、requests
request的说法网上有很多,简单来说就是就是python里的很强大的类库,可以帮助你发很多的网络请求,比如get,post,put,delete等等,这里最常见的应该就是get和post
二、requests安装方式
$ pip install requests $ easy_install requests
三、说说常见的两种请求,get和post
1、get请求
(1)参数直接跟在url后面,即url的“ ?”后面,以key=value&key=value的形式
(2)由于get的参数是暴露在外面的,所以一般不传什么敏感信息,经常用于查询等操作
(3)由于参数是跟在url后面的,所以上传的数据量不大
2、post请求
(1)参数可以写在url后面,也可以写在body里面
(2)用body上传请求数据,上传的数据量比get大
(3)由于写在body体里,相对安全
post正文格式
(1)form表单 html提交数据的默认格式
Content-Type: application/x-www-form-urlencoded
例如: username=admin&password123
(2) multipart-form-data . 复合表单 可转数据+文件
(3)纯文本格式 raw ,最常见的 json . xml html js
Content-Type:application/json . text/xml . text/html
(4) binary . 二进制格式:只能上传一个文件
四、requests发送请求
1、requests发送get请求
url = "http://www.search:9001/search/" param = {"key":"你好"} res = requests.get(url=url, params=params)
2、request发送post请求 (body是json格式,如果还带cookie)
headers = {'Content-Type': 'application/json'} #必须有 url = "http://www.search:9001/search/" data= {"key":"你好"} cookies = {"uid":"1"} res = requests.post(url=url, headers=headers, data=data, cookies=cookies)
3、 request发送post请求 (body是urlencoded格式)
url = "http://www.search:9001/search/" data= {"key":"你好"} res = requests.post(url=url, headers=headers)
4、 request上传文件
def post_file_request(url, file_path): if os.path.exists(file_path): if url not in [None, ""]: if url.startswith("http") or url.startswith("https"): files = {'file': open(file_path, 'rb')} res = requests.post(url, files=files, data=data) return {"code": 0, "res": res} else: return {"code": 1, "res": "url格式不正确"} else: return {"code": 1, "res": "url不能为空"} else: return {"code": 1, "res": "文件路径不存在"}
五、response
request发送请求后,会返回一个response,response里有好多信息,我进行了一下封装,基本如下
@staticmethod def get_response_text(response): if response not in [None, ""]: if isinstance(response, requests.models.Response): return {"code": 0, "res": response.text.encode('utf-8').decode('unicode_escape')} #这种方式可以将url编码转成中文,返回响应文本 else: return {"code": 1, "res": "response不合法"} else: return {"code": 1, "res": "response对像不能为空"} @staticmethod def get_response_status_code(response): if response not in [None, ""]: if isinstance(response, requests.models.Response): return {"code": 0, "res": response.status_code} #返回响应状态吗 else: return {"code": 1, "res": "response不合法"} else: return {"code": 1, "res": "response对像不能为空"} @staticmethod def get_response_cookies(response): if response not in [None, ""]: if isinstance(response, requests.models.Response): return {"code": 0, "res": response.cookies} #返回cookies else: return {"code": 1, "res": "response不合法"} else: return {"code": 1, "res": "response对像不能为空"} @staticmethod def get_response_headers(response): if response not in [None, ""]: if isinstance(response, requests.models.Response): return {"code": 0, "res": response.headers} #返回headers else: return {"code": 1, "res": "response不合法"} else: return {"code": 1, "res": "response对像不能为空"} @staticmethod def get_response_encoding(response): if response not in [None, ""]: if isinstance(response, requests.models.Response): return {"code": 0, "res": response.encoding} #返回编码格式 else: return {"code": 1, "res": "response不合法"} else: return {"code": 1, "res": "response对像不能为空"}
补充:requests中遇到问题
获取cookie
# -*- coding:utf-8 -*- #获取cookie import requests import json url = "https://www.baidu.com/" r = requests.get(url) #将RequestsCookieJar转换成字典 c = requests.utils.dict_from_cookiejar(r.cookies) print(r.cookies) print(c) for a in r.cookies: print(a.name,a.value) >> 控制台输出:]> {'BDORZ': '27315'} BDORZ 27315
发送Cookie
# -*- coding:utf-8 -*- #发送cookie到服务器 import requests import json host = "*****" endpoint = "cookies" url = ''.join([host,endpoint]) #方法一:简单发送 # cookies = {"aaa":"bbb"} # r = requests.get(url,cookies=cookies) # print r.text #方法二:复杂发送 s = requests.session() c = requests.cookies.RequestsCookieJar() c.set('c-name','c-value',path='/xxx/uuu',domain='.test.com') s.cookies.update(c)
总结
到此这篇关于python中Requests请求的安装与常见用法的文章就介绍到这了,更多相关python中Requests请求内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!