def parse(self, response):
file_name = 'teachers.html'
with open(file_name, 'w+') as f:
f.write(response.body)
Traceback (most recent call last):
File "d:\python\lib\site-packages\twisted\internet\defer.py", line 653, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "E:\xampp\htdocs\my_sp\my_sp\spiders\my_sp_one.py", line 13, in parse
f.write(response.body)
TypeError: write() argument must be str, not bytes
- 搜索之后发现只需要将写入模式改为 'wb+' 就不会报错了
注意,Python2.x默认编码环境是ASCII,当和取回的数据编码格式不一致时,可能会造成乱码
我用的python3,没有这个问题,因为python3默认编码是Unicode