初级版本
源码:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#使用车管家的接口实际调用类函数
import unittest
from urllib import request
from urllib import parse
import json
#构造成函数用于调用
def req(url,headers,data):
url = url
headers = headers
data = data
data = parse.urlencode(data).encode('utf-8')
req = request.Request(url,headers=headers,data=data)
page = request.urlopen(req).read()
page = page.decode('utf-8')
#返回page
#return page
#将json格式转换为python的数据结构,并打印出关键值
#return sorted(json.loads(page)['data']['recommendTitle'].split(','))
#print(json.loads(page)['code'])
return page
#return json.loads(page)['code']
class TestReq(unittest.TestCase):
#3.首页-自助保养--获取所有的保养项目列
def test_allmaintenance(self):
#用例1:根据公里数获取保养项目(mileages为210000,超出了最大的保养公里数值)
page = req('http://cargw.jd.com/mClient/maintenance/allMaintenance?',{'Referer':'http://car.m.jd.com/h5/selfCareInit.html','User-Agent':'Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Mobile Safari/537.36'},{'modelId':8197,'provinceCode':'','cityCode':1930,'areaCode':'','sid':'','lng':103.99318404013731,'lat':30.619026814469912,'area':1_2809_51216_0,'area1':1,'area2':2809,'area3':51216,'area4':0})
self.assertEqual('0',json.loads(page)['code'])
你可以根据自己的业务模仿着写哦。