python学习笔记(十四)-- requests接口调用

接口调用#发网络请求的模块

import requests

#get请求
#params写在url中的参数
#req = requests.get(url=url,params=data,headers=header) url='http://api.nnzhp.cn/api/user/stu_info' d = {'stu_name':'矿泉水2'} req = requests.get(url,d) print(req.json())#字典 print(req.text)#字符串 #post请求
#requests.post(url=url,data=data,headers=header,cookies=cookie,files=file) url = 'http://api.nnzhp.cn/api/user/login' data = {'username':'niuhanyang','passwd':'aA123456'} req = requests.post(url,data) print(req.json()) #cookie url='http://api.nnzhp.cn/api/user/gold_add' data = {'stu_id':2,'gold':111111} cookie = {'niuhanyang':'5dab62839d51c0799c6ddb1476d6a511','xxx':'xxx'} cookie2 = {'cookie':'RK=JQZpwBp1by; ptcz=6c30e26a9ed6be93d3de9e4c4aca3e55650cf99fcffa64729bd1d58a5fb209d9; pgv_pvi=779236352; pgv_pvid=6970909788; qb_qua=; qb_guid=818de686e29d412fa4ee9e99905ea166; Q-H5-GUID=818de686e29d412fa4ee9e99905ea166; NetType=; o_cookie=511402865; pac_uid=1_511402865; uin=o0511402865; ptisp=cnc; pgv_si=s3027380224; skey=@gfJBoFjZ4; p_uin=o0511402865; pt4_token=WFIECK07PiBaZz-SLJfuHiRBNMzM9ktkgImEdNSchgg_; p_skey=QgOGMs9jcRBKhd0z6ha3Tk0UhrUz8nQ3A2fJ9pkxsAE_; traceid=a5329de24a'} req = requests.post(url,data,cookies=cookie) req = requests.post(url,data,headers=cookie2) print(req.text) #入参是json的 data = { "name": "矿泉水33333", "grade": "双子座", "phone": "12123624321", "sex": "未知", "addr": "天通苑", "age": 38 } url = 'http://api.nnzhp.cn/api/user/add_stu' req = requests.post(url,json=data) print(req.text) #上传附件 url='http://api.nnzhp.cn/api/file/file_upload' data = {'file':open('上周作业.py','rb')} req = requests.post(url,files=data) print(req.text) #以二进制写入 url='https://aliimg.changba.com/cache/photo/18189396_640_640.jpg' req = requests.get(url) with open('aqmm.jpg','wb') as fw: fw.write(req.content)

 

posted @ 2019-05-16 13:08  颜颜blog  阅读(307)  评论(0编辑  收藏  举报