Python安全笔记(二)

“TypeError: write() argument must be str, not bytes”错误

出现以上错误的代码:

r = requests.get("https://www.baidu.com",verify=False)
cont = r.content
o = open("test.html",'w')
o.write(cont)

原因猜测:因为open以写模式打开文件时,默认是设该文件为strings格式,因此write的参数只能为str格式,而r.content是byte格式,二者格式不一致
修复:open的参数改为“wb”,即以二进制格式打开,则前后的格式一致,执行成功

r = requests.get("https://www.baidu.com",verify=False)
cont = r.content
o = open("test.html",'wb')
o.write(cont)

猜你喜欢

转载自blog.csdn.net/ZHOUYAO_NJUST/article/details/84955599