import requestsclass TestCase:# 设置1个类变量B = "初始值"def test1(self):url = "**这里是接口url**"params = {"type": "json"}resp1 = requests.get(url=url, params=params)# .json()用于将服务器返回的 JSON 格式的响应内容解析为 Python 对象data = resp1.json()print(data)A = data["data"]["annotate"]print(A)# 将返回的draw值,赋值给类变量BTestCase.B = data["data"]["draw"]def test2(self):print("B的值为" + TestCase.B)if __name__ == "__main__":TestCase().test1()# 由于test2在test1之后运行,所以B的值为test1最终赋予的值TestCase().test2()
本次接口的返回内容:
{"code": 1,"text": "获取成功","data": {"format": 77,"draw": "捧璧归赵 (中签)","annotate": "此卦梦中得宝之象,凡事虚多少实也。","explain": "如梦说梦 声名莫望 只待好时 贵人指示","details": "家宅祈福\u3000移徙待时\u3000六甲虚\u3000公讼和\u3000交易难成\u3000疾病殁送\u3000行人迟\u3000失物远\u3000婚姻不合\u3000自身阻\u3000六畜损\u3000山坟改\u3000田蚕不收\u3000求财虚\u3000寻人杳","source": "完璧归赵。春秋时代。秦强赵弱。秦王欲十五城交换和氏璧。赵国大夫蔺相如携璧至秦。见秦王欲占不换此璧。蔺相如以砸碎和氏璧相要。最后终可携璧归赵。","image": "http://shanhe.kim/api/za/data/chouq/image/77.gif",},
}