在运行文件操作相关功能时报错:ValueError: binary mode doesn't take an encoding argument
即不支持指定encoding参数。
import requests
response = requests.get(url='http://www.baidu.com')
print(response.status_code) #打印状态码
print(response.url) #请求的url
print(response.headers) #打印头部信息
print(response.cookies) #打印cookies信息
print(response.text) #打印源代码
print(response.content) #以字节流打印bytes
with open('a.txt','wb',encoding='utf-8') as f:
f.write( str(response.content))
"""
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "D:\pycharm2020\PyCharm 2020.1\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "D:/python_files/爬虫/初始_02.py", line 10, in <module>
with open('a.txt','wb',encoding='utf-8') as f:
ValueError: binary mode doesn't take an encoding argument
"""
原因分析:rb+,wb等操作时不支持指定encoding参数,删除encoding即可:
with open('a.txt','w+') as f:
f.write( str(response.content))