1 import jsonpath,json,requests,nnlog
2
3 #例子
4 s ={
5 "error_code": 0,
6 "stu_info": [
7 {
8 "id": 1,
9 "name": "xmb",
10 "sex": "男",
11 "age": 9,
12 "addr": "深圳",
13 "grade": "三年级",
14 "phone": "1860000000",
15 "gold": 1000
16 }
17 ]}
18 result = jsonpath.jsonpath(s,'$.stu_info[0].id')
19 print(result)
20 result = jsonpath.jsonpath(s,'$..id')
21 print(result)
22
23 #request练习
24 log = nnlog.Logger('test.log',level='debug',backCount=5,when='D')
25 def getSign():
26 url = 'http://127.0.0.0/user/login'
27 data = {"username":"xmb","passwd":"aA123456"}
28 #result = requests.post(url,data).text
29 result = requests.post(url,data).json()
30 print(result)
31 log.debug(result) #写日志
32 sign = jsonpath.jsonpath(result,'$..sign') #取返回值的sign值
33 log.info(sign) #写日志
34 print(sign)
35
36 if __name__ == '__main__':
37 getSign()