将reponse内容显示为中文,再将其str转为dict
res=response.content.decode("unicode_escape")
res_dict=json.loads(res)
获取token(看是localStorage还是SessionStorage),需要sleep一会等待token传过来才可以获取到
token = self.driver.execute_script('return localStorage.getItem("token");')
print('\ntoken为:%s\n'%token)
获取cookie
#获取cookie
cookie_tag = self.driver.get_cookies()
# print(cookie_tag)
#处理cookie成需要的格式
cookie = cookie_tag[0]['name'] + '=' + cookie_tag[0]['value']
print('\ncookie为:%s\n'%cookie)
读取、写入文件乱码
###读取
f=open('{}'.format(file_catalogue+'/'+file_list[0]),mode='r',encoding='utf-8',errors='ignore')
contentJson=json.load(f)
print(contentJson)
###写入
with open('student.json',mode='w',encoding='utf8') as f:
json.dump(jsonA,f,ensure_ascii=False,index=2) #ensure_ascii=False不让汉字转为ASCII
#f.write(jsonA) #不允许用普通文件的方式写入 因为后缀是json
将字典更方便查看
print(json.dumps(dict_data,indent=1,ensure_ascii=False))