get与post区别
''' 当url为get方法,且使用params传递参数,执行结果 相同点:使用get和post都可以请求和响应成功 ''' def test_getmethod_usepost(): payload={'q':'电影'} url='https://www.douban.com/search' #get请求 res1=requests.get(url,params=payload) print("res1_url****"+res1.url) #输出结果 . res1_url****https://www.douban.com/search?q=%E7%94%B5%E5%BD%B1 print(res1.text) res2=requests.post(url,params=payload) print("res2_url****"+res2.url) #输出结果 res2_url****https://www.douban.com/search?q=%E7%94%B5%E5%BD%B1 print(res2.text) ''' 当url为post方法,且需要添加请求body,执行结果 1、使用get请求失败,请求时不会拼接请求体 . res_url****https://accounts.douban.com/j/mobile/login/basic <bound method Response.json of <Response [403]>> 2、使用post,请求体使用params添加请求body,请求失败 . res_url****https://accounts.douban.com/j/mobile/login/basic?ck=&name=18500910544&password=Echoque0612&remember=False&ticket= <bound method Response.json of <Response [200]>> 用户名或密码错误 ''' def test_post(): mypayload = {'ck': '', 'name': 'xxxx','password':'xxxxx','remember':False,'ticket':''} myheader={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36'} url='https://accounts.douban.com/j/mobile/login/basic' #post方法传入请求体使用data或json res=requests.get(url,verify=False,headers=myheader,data=mypayload) #异常 # res=requests.post(url,verify=False,headers=myheader,data=mypayload) #正常 # res=requests.post(url,verify=False,headers=myheader,params=mypayload) #异常 print("res_url****"+res.url) print(res.json) print (res.json().get('description')) assert res.json().get('description')=='处理成功' ```

更多精彩