码迷,mamicode.com
首页 > 编程语言 > 详细

python学习笔记-调用接口

时间:2018-05-18 23:28:58      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:span   河南省   class   passwd   jni   on()   top   接口   aaa   

requests模块

1.get请求:

url = http://api.nnzhp.cn/api/user/stu_info
data = {stu_name:小黑}  #请求数据 req = requests.get(url,params=data)  #发get请求
print(req.json())  #字典
print(req.text)    #string,json串

 

2.post请求:

url = http://api.nnzhp.cn/api/user/login
data = {username:niuhanyang,passwd:aA123456}  #请求数据
req = requests.post(url,data)  #发送post请求
print(req.json())

 

3.入参是json类型:

import random
phone=random.randint(10000000000,99999999999)
url=http://api.nnzhp.cn/api/user/add_stu
data =  {
    "name":"小1",
    "grade":"天蝎座",
    "phone":phone,
    "sex":"",
    "age":28,
    "addr":"河南省济源市北海大道32号"
  }
req = requests.post(url,json=data)
print(req.json())

 

4.添加cookie:

url = http://api.nnzhp.cn/api/user/gold_add
data = {stu_id:468,gold:10000}
djl = {niuhanyang:337ca4cc825302b3a8791ac7f9dc4bc6}
req = requests.post(url,data,cookies=djl)
print(req.json())

 

5.添加header:

url = http://api.nnzhp.cn/api/user/all_stu
header = {
    Referer:http://api.nnzhp.cn/
}
req = requests.get(url,headers=header)
print(req.json())

 

6.上传文件:

url= http://api.nnzhp.cn/api/file/file_upload
data = {
    file:open(rC:\Users\bjniuhanyang\Desktop\图\6bd9026dt935575932465&690.jpg,rb)
}
req= requests.post(url,files=data)
print(req.json())

 

7.下载文件:

url = http://up.mcyt.net/?down/46779.mp3
req = requests.get(url)
fw = open(aaa.mp3,wb)
fw.write(req.content)

 

python学习笔记-调用接口

标签:span   河南省   class   passwd   jni   on()   top   接口   aaa   

原文地址:https://www.cnblogs.com/huangr/p/9057284.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!