import requests;
import json;
url=“http://hassan.bilibili.co/ruler/rule/query”
Cookie= “username=mazhenghang;_AJSESSIONID=01f91531f01a53e11f8f8320bddb5b06”
headers={‘content-type’:“application/json”,“cookie”:Cookie}
data={“page_size”: 100, “page_num”: 1, “id”: 0, “env”: “”}
env=input(“请输入mock环境名称:”)
data[“env”]=env
r = requests.post(url, data=json.dumps(data),headers=headers) #将python数据结构转换为JSON ——json.dumps(data)函数
myJson=r.text #读取文本
mapObject=json.loads(myJson)
iterObject=iter(mapObject[“data”][“rules”]) #iter(Object),object必须是集合对象
mydict={}
with open(“MockRulesTxt”,“w”) as f:
for ele in iterObject:
ID = ele.get(“id”)
comment = ele.get(“comment”)
mydict[comment]=ID;
mockRulesJson=json.dumps(mydict,ensure_ascii=False) #json文件显示中文
f.write(mockRulesJson) #将json写入
print(“MockRules已在本脚本文件同目录下的 MockrulesTxt中!”)