爬虫小操作

将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))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值