标签:type create lob pass ret login from pip get
Requests是用python语言基于urllib编写的,采用的是Apache2 Licensed开源协议的HTTP库 如果你看过上篇文章关于urllib库的使用,你会发现,其实urllib还是非常不方便的,而Requests它会比urllib更加方便,可以节约我们大量的工作。(用了requests之后,你基本都不愿意用urllib了)一句话,requests是python实现的最简单易用的HTTP库,建议爬虫使用requests库。 默认安装好python之后,是没有安装requests模块的,需要单独通过pip安装。
安装方式很简单,直接使用命令安装即可,如下:
pip install requests
也可直接在PyCharm工具中安装,相信大家都已轻车熟路,就忽略了。
requests里提供了各种请求方式,比如:get,post,delete,put,具体如下所示:
import requests
requests.post("http://httpbin.org/post")
requests.put("http://httpbin.org/put")
requests.delete("http://httpbin.org/delete")
requests.get("http://httpbin.org/get")
在使用requests时,无论你使用的请求方法是get也好,post也罢,他们都会给你返回一个响应的对象,而针对这个对象有一些特定的方法,能够让你进一步解析响应从而得到你想要的数据,如下:
response.json() # 以json的形式返回响应内容,对象格式为dict response.content # 以二进制的形式返回响应内容,对象格式为bytes response.text # 以字符串的形式返回响应内容,对象格式为str response.url # 返回请求的url response.status_code # 返回本次请求的状态码 response.reason # 返回状态码对应的原因 response.headers # 返回响应头 response.cookies # 返回cookice信息 response.raw # 返回原始响应体 response.encoding # 返回编码格式
我们了解了基本的请求方式以及响应对象,接下来我们就来以实例再来实践一番,加深印象以及更好的理解。
import requests # 构建一个get请求的对象 response = requests.get(‘http://www.baidu.com‘) # 以字符串的形式返回响应的内容 print response.text
import requests # 基本get请求 r = requests.get(url=‘http://www.baidu.com‘) # 最基本的GET请求 print(r.status_code) # 获取返回状态 r = requests.get(url=‘http://www.baidu.com/s‘, params={‘wd‘: ‘温一壶清酒 博客园‘}) # 带参数的GET请求 print(r.url) print(r.text) # 打印解码后的返回数据
我们也可以把参数放到一个变量中传递,如下:
import requests params = { ‘wd‘: ‘温一壶清酒 博客园‘ } # 基本get请求 r = requests.get(url=‘http://www.baidu.com‘) # 最基本的GET请求 print(r.status_code) # 获取返回状态 r = requests.get(url=‘http://www.baidu.com/s‘, params=params) # 带参数的GET请求 print(r.url) print(r.text) # 打印解码后的返回数据
以普通登录方式为例,如下所示:
import requests # 普通登录 headers = {‘Authorization‘: ‘‘} r = requests.post(url=‘XXXX‘, data={ ‘username‘: ‘‘, ‘password‘: ‘‘},headers=headers) print(r.url) print(r.status_code) # 获取返回状态 print(r.text) # 打印解码后的返回数据
import requests from requests.auth import HTTPBasicAuth # basic auth方式登录 # 方法一 a = HTTPBasicAuth(‘‘, ‘‘) r = requests.post(url="XXXXXXX", data={‘grant_type‘: ‘‘, ‘scope‘: ‘‘},auth=a) print(r.status_code) # 获取返回状态 print(r.text) # 打印解码后的返回数据
import requests # basic auth方式登录 # 方法二 (只加载requests模块就行) r = requests.post(url=‘XXXXXXXX‘, data={‘grant_type‘: ‘‘, ‘scope‘: ‘‘},auth=(‘‘, ‘‘)) print(r.status_code) # 获取返回状态 print(r.text) # 打印解码后的返回数据
如普通登录post请求方式所示,就添加了headers参数,不同的应用场景,headers里面的参数也就各不同。示例参见第五点即可。
有些网址访问时,需要校验证书,比如12306官网,我们先来简单访问下,如下:
import requests # 证书验证 r = requests.get("https://www.12306.cn") print(r.status_code) print(r.text)
运行代码,报错如下:
在网上寻找一番,可以得到答案,如下所示:
import requests import urllib3 # 证书验证 urllib3.disable_warnings() r = requests.get("https://www.12306.cn", verify=False) print(r.status_code) print(r.text)
再次访问,就可以正常进入官网了。
比如下登录操作时,返回的json数据,在控制台中是一行展示,如果数据过多,则不便于查看,如下所示:
使用如下代码实现:
import requests import json def better_output(json_str): return json.dumps(json.loads(json_str), indent=4) url = "XXXXXX" headers = {‘Authorization‘: ‘‘} request_param = { ‘username‘: ‘‘, ‘password‘: ‘‘ } response = requests.post(url, data=request_param, headers=headers) print(better_output(response.text))
再次运行代码,查看json数据,如下所示,这样就很方便查看了。
我们实现登录之后,继续做下一步操作,那就需要传递token了,不然会报401错误,没有权限。接下来就来看下如何实现:
import requests import json host = ‘‘ # json数据转换格式,ensure_ascii显示为中文 def better_output(json_str): return json.dumps(json.loads(json_str), indent=4, ensure_ascii=False) def login(): # 普通登录 headers = {‘Authorization‘: ‘‘} data = { ‘username‘: ‘‘, ‘password‘: ‘‘ } url = host + ‘/token‘ r = requests.post(url=url, data=data, headers=headers, verify=False) # 设置全局变量 global token token = (r.json()["access_token"]) print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++") print("+++登录接口响应的状态码为:" + str(r.status_code)) # 获取返回状态 print("+++登录接口响应的数据为:" + better_output(r.text)) # 打印解码后的返回数据 print("+++获取到的token值为:" + token) print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++") login()
将token设置成全局变量,便可在各接口间传递,token已经获取到,将token传到headers中即可,如下:
headers = { ‘Content-Type‘: ‘application/json;charset=utf-8‘, # token传参 ‘Authorization‘: ‘Bearer ‘ + token }
import requests,json url = "http://xxx" headers = {"Content-Type":"application/json","Authorization":"Bearer token值"} data1 = {"key1":"value1","key2":"value2"} data = {"params":data1} r = request.post(url = url,data = json.dumps(data),headers = headers)
import requests,json url = "http://xxx" headers = {"Content-Type":"application/json","Authorization":"Bearer token值"} data1 = {"key1":"value1","key2":"value2"} data = {"params":data1} r = request.post(url = url,json = data,headers = headers)
如下讲述了这么多,就统一来个综合实践,里面包含了动态传参,添加请求头,json入参嵌套等,如下所示:
import requests import json host = ‘‘ # json数据转换格式,ensure_ascii显示为中文 def better_output(json_str): return json.dumps(json.loads(json_str), indent=4, ensure_ascii=False) def login(): # 普通登录 headers = {‘Authorization‘: ‘‘} data = { ‘username‘: ‘‘, ‘password‘: ‘‘, } url = host + ‘/token‘ r = requests.post(url=url, data=data, headers=headers, verify=False) # 设置全局变量 global token token = (r.json()["access_token"]) print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++") print("+++登录接口响应的状态码为:" + str(r.status_code)) # 获取返回状态 print("+++登录接口响应的数据为:" + better_output(r.text)) # 打印解码后的返回数据 print("+++获取到的token值为:" + token) print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++") def search_gender_data(): # 搜索接口 headers = { ‘Content-Type‘: ‘application/json;charset=utf-8‘, # token传参 ‘Authorization‘: ‘Bearer ‘ + token } url = host + ‘/api‘ r1 = requests.get(url=url, headers=headers) global gender gender = r1.json()[‘values‘][0][‘code‘] print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++") print("+++搜索接口响应的状态码为:" + str(r1.status_code)) # 获取返回状态 print("+++搜索接口响应的数据为:" + better_output(r1.text)) print("+++获取的性别code为:" + gender) print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++") def create_user(): headers = { ‘Content-Type‘: ‘application/json;charset=utf-8‘, # token传参 ‘Authorization‘: ‘Bearer ‘ + token } data = { "duty": "", "dutyCode": "", "rank": "", "rankCode": "", "deleted": ‘false‘, "userId": "", "employeeId": "", "position": "测试负责人", "companyMainPosition": ‘true‘, } data1 = { "departmentPath": "", "directManager": "", "directManagerId": ‘‘, "directManagerName": "", "employeeType": ‘‘, "email": "10150001@qq.com", "mobile": "15110151015", "mobileCode": "86", "countryCode": "CN", "employeeTypeCode": "", "userOID": ‘‘, "employeeID": "10150001", "fullName": "10150001接口新增", "status": 1001, "manager": ‘false‘, "leavingDate": "3018-01-31T16:00:00.000Z", "gender": "", "genderCode": gender, "birthday": "", "entryTime": "", "customFormValues": [], ‘userJobsDTOs‘: [data] } url = host + ‘/api‘ create_u = requests.post(url=url, json=data1, headers=headers) print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++") print("+++创建人员接口url为:" + url) print("+++创建人员接口响应的状态码为:" + str(create_u.status_code)) # 获取返回状态 print("+++创建人员接口响应的数据为:" + better_output(create_u.text)) print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++") if __name__ == ‘__main__‘: # 调用函数 login() search_gender_data() create_user()
Requests实践详解希望对有需要的朋友有所帮助,都是些基本的操作,博客书写,也是我自己学习的一种方式。文中描述有误的地方,欢迎批评指正。
本文仅代表作者观点,系作者@温一壶清酒发表。
欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
文章出处:http://www.cnblogs.com/hong-fithing/
标签:type create lob pass ret login from pip get
原文地址:https://www.cnblogs.com/hong-fithing/p/11679676.html