用了很多方法都没有这个实用
POST API接口:
import json
import requests
if __name__ == '__main__':
url = "http://127.0.0.1:9001/rsa/set_uplod"
headers = {'content-type': 'application/json'}
requestData = {"certificate_no": "56565656565656", "auth_code": "123456"}
ret = requests.post(url, json=requestData, headers=headers)
if ret.status_code == 200:
text = json.loads(ret.text)
print(text)
get API 接口:
import json
import requests
if __name__ == '__main__':
url = "http://127.0.0.1:9001/getSha1Test?message=APP000051527151133935949D49FDC0A86605313931B9BB9A4C87"
ret = requests.get(url)
if ret.status_code == 200:
text = json.loads(ret.text)
print(text)
假如我们知道豆瓣读书的API接口,https://api.douban.com/v2/book/1220563
想要调用JSON接口。
则使用如下代码实现:
# #coding:utf-8
import json
import urllib2
#若果只需要获取一个接口的数据
url = r'https://api.douban.com/v2/book/1220563'
#访问网页
response = urllib2.urlopen(url)
#json解析
html = json.loads(response.read())
#打印所需数据,我这里只打印了书名
print "题目:","《",html['title'],"》"
输出:
题目: 《 从胎教开始 》
1
当然如果你想获取多个JSON数据,加个循环就可以了:
# #coding:utf-8
import json
import urllib2
#参数我是随便加的,只要符合网址规则就行
for i in range(1220564,1220580):
#url地址
url = 'https://api.douban.com/v2/book/'
#将参数转化为字符串,方便组装url
values = str(i)
#进行参数封装
url = url + values
#访问完整url
response = urllib2.urlopen(url)
#读取网页内容
html = json.loads(response.read())
#打印所需数据
print "出版日期:",html['pubdate']
print "概述:",html['summary']
print "作者:",html['author'][0]
print "价格:",html['price']
print "---------------------------------------------------"
i = i + 1
————————————————
格式化返回:https://www.cnblogs.com/jingdenghuakai/p/11805013.html