正则取值及断言实战示例

demo_01.py

import requests
import re
session = requests.session()
get_param_dict={
"grant_type":"client_credential",
"appid":"wx55614004f367f8ca",
"secret":"65515b46dd758dfdb09420bb7db2c67f"
}
response = session.get( url='https://api.weixin.qq.com/cgi-bin/token',
params=get_param_dict)
response.encoding = response.apparent_encoding
body = response.text
print( body )
value = re.findall('"access_token":"(.+?)"',body)[0]
print( value ) 

post方法:

接口断言设计:

断言设计思路:

检查多个key是否存在:

两个都是true,说明2个都存在。

这里至始至终要保证出一个结果:万一结果覆盖或者出现其他情况就不对。处理:

这种也不对:在循环第一次的时候不满足是false,循环第2次的时候是满足的是true.因为检查的是多个key,所以把结果覆盖掉了,就不对。这种方法无法判断yes和no。

调整:当它等于access_toke不包含n的,for循环检查不到的时候就在列表yes_no =[]里面增加一个false,当它满足expires_in的时候就增加一个true。

不管我们这里检查了多少个key,如果检查的过程中出现了一次,一种,或者一个false,那么这次检查就是失败的。

回忆滋润坚持
原文地址:https://www.cnblogs.com/james5d/p/14116854.html