import requests 导入requests库

1、基本信息

  1. 请求地址:url
  2. 协议状态码:status_code
  3. 请求头:headers
  4. 响应内容:text
  5. 二进制文件:content
  6. json格式响应文件:json()
  7. 编码:encoding
  8. cookiescookies
  9. 请求响应时间:elapsed.total_seconds()
  10. json排序:json.dumps(r.json(),indent=True,ensure_ascii=False)
  11. 认证证书:verify=True

2、拉勾网post请求

  1. headers = {'Cookie':'123456789',
  2. 'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36 Edg/86.0.622.61',
  3. 'referer':'https://www.lagou.com/jobs/list_%E6%B5%8B%E8%AF%95%E5%B7%A5%E7%A8%8B%E5%B8%88/p-city_0?&cl=false&fromSearch=true&labelWords=&suginput=',
  4. 'content-type':'application/x-www-form-urlencoded; charset=UTF-8'
  5. }
  6. params = {'needAddtionalResult':False}
  7. data={'first':False,'pn':2,'kd':'测试工程师'}
  8. r = requests.post(url='https://www.lagou.com/jobs/positionAjax.json',
  9. headers=headers,
  10. params=params,
  11. data=data)
  12. print(r.text)

3、data与json的区别

content-type’:’application/x-www-form-urlencoded; charset=UTF-8 and application/json——>发送的是json格式的字符串
data传输的是form表单数据,json传输的是json数据格式
json要满足两个条件:
1、application/json
2、请求参数必须是字典

4、超时处理

在请求中加上timeout

  1. r = requests.post(url='http://127.0.0.1:5000/v1/api/books',json=json,timeout=60)

5、复杂参数处理

  1. datas={'name':'super','age':18,'data':[{'address':'wuhan'},{'address':'hubei'}]}
  2. #先把列表反序列化成字符串
  3. datas['data']=json.dumps(datas['data'])

6、token与Sessioon处理

  1. 1、登录获取token
  2. def login(self):
  3. data = {'username':'wuya','password':'asd888'}
  4. r = requests.post(url='http://127.0.0.1:5000/auth',json=data)
  5. return r.json()['access_token']
  6. 2、放入headers头中
  7. headers={'Authorization':'jwt {0}'.format(self.login())}
  8. #登录获取cookies
  9. def login(self):
  10. data={'id':'admin','password':'AdJUrorlWKjshy+8ygM5KQ==','authtimeStamp':'1607350740336'}
  11. headers={''}
  12. r=requests.post(url='http://iotwedora.com/monitor/login',
  13. data=data,
  14. headers=headers)
  15. return r.cookies
  16. #获取标题信息
  17. def title(self):
  18. headers = {''}
  19. r = requests.get(url ='http://iotwedora.com/monitor/device/manager/list-fields',
  20. headers=headers,
  21. cookies=self.login()) #重点
  22. print(json.dumps(r.json(),indent=True,ensure_ascii=False))

7、sessions会话对象

  1. import json
  2. import requests
  3. class Test():
  4. #登录获取cookies
  5. def login(self):
  6. s = requests.Session() #重点
  7. data={'id':'admin','password':'AdJUrorlWKjshy+8ygM5KQ==','authtimeStamp':'1607350740336'}
  8. headers={''}
  9. r=s.post(url='http://iotwedora.com/monitor/login',
  10. data=data,
  11. headers=headers)
  12. return s
  13. #获取标题信息
  14. def title(self):
  15. headers = {''}
  16. r = self.login().get(url ='http://iotwedora.com/monitor/device/manager/list-fields',
  17. headers=headers)
  18. print(json.dumps(r.json(),indent=True,ensure_ascii=False))
  19. if __name__ == '__main__':
  20. r = Test()
  21. r.title()

8、鉴权认证

  1. from requesta.auth import HTTPBasicAuth
  2. auth=('账户','密码')

9、文件上传

  1. files={'file':(文件名称,open(文件路径,'rb'),文件类型,{})}
  2. files={'file':('log.jpg',open('c:/log.jpg','rb'),'image/jpeg',{})}