标签:pre 返回 app nbsp try remember requests def 提取
删除多个目录
删除每个目录下的对话主题
# -*- coding: utf-8 -*- # @Time : 2020/2/11 12:00 # @File : login_chat.py # @Author: Hero Liu # 连续删除对话主题 import requests import json Cookie = None class HttpRequest: def http_request(self, url, method, data=None, cookie=None): res = None try: if method.upper() == "GET": res = requests.get(url, data, cookies=cookie) elif method.upper() == "POST": res = requests.post(url, data, cookies=cookie) else: print("请输入正确参数") except Exception as e: print("请求报错了:{}".format(e)) raise e return res def get_dir(file, key, sub_key): # 提取json数据的所有nodeId # 需要编码,不然可能会报错 # 这里的json数据是这样的格式{"children":[{"children":[],"nodeId":""},{},{}],"xxx":"xxx"} with open(file, "r", encoding="utf-8") as f: # 获取文件 json_str = f.read() # 获取文件内容 data = json.loads(json_str) dir_list = [] # data[key]是这里的第一个children,它是一个列表,列表的每个元素是一个字典 for sub_dic in data[key]: # sub_dic[sub_key]是每一个字典的键"nodeId" dir_id = sub_dic[sub_key] # 把每一个字典的键"nodeId"加到一个列表里 dir_list.append(dir_id) # 返回的列表含指定层级下的所有"nodeId" return dir_list if __name__ == ‘__main__‘: # 登录 login_url = "http://chat.rainbowred.com/login" login_data = {"username": "15546355872", "password": "123456", "rememberCheck": "1", "loginStatus": "1", "rememberStatus": "1", "autoLogin": "0", "language": "zh"} login_res = HttpRequest().http_request(login_url, "post", login_data) print(login_res.json()) Cookie = login_res.cookies print("登录后的cookie", Cookie) # 选择公司 c_url = "http://chat.rainbowred.com/chc" c_data = {"companyId": "1364"} c_res = HttpRequest().http_request(c_url, "post", c_data, cookie=Cookie) print(c_res.json()) print("选择公司后的cookie", c_res.cookies) # 合并两次接口请求的cookie Cookie = dict(Cookie, **c_res.cookies) print("选择公司后的cookie2", Cookie) file = r"C:\Users\acer-pc\Desktop\8_.json" key = "children" sub_key = "nodeId" # 删除对话主题的接口 d_url = "http://tprofile.rainbowred.com/ctm/delete" # d_data是"nodeId"的集合 d_data = get_dir(file, key, sub_key) send_data = {} # 提取json数据,即传入每一个"nodeId",发起删除请求 for data in d_data: print(data) send_data["nodeId"] = data d_res = HttpRequest().http_request(d_url, "post", send_data, Cookie) print(d_res.json())
标签:pre 返回 app nbsp try remember requests def 提取
原文地址:https://www.cnblogs.com/come202011/p/12335491.html