我正在尝试创建2个文件。首先,回应.png(有效)。其次,取所有html并保存到文本。将html保存到文本文件只显示为空白。
def parse_request(self, response):
# Works and saves file and png
with open('image.png', 'wb') as f:
f.write(response.body)
# Creates file but doesnt have any text in it
with open('text.txt', 'wb') as f:
f.write(response.body.decode("utf-8"))
答案 0 :(得分:0)
您是否尝试将response.body
存储到变量,然后write
该变量?
我认为你在第一次阅读时耗尽了流的反应。我也猜测你可能存储整个响应,而不仅仅是image.png
文件中的png。
def parse_request(self, response):
body = response.body
# Works and saves file and png
with open('image.png', 'wb') as f:
f.write(body)
# Creates file but doesnt have any text in it
with open('text.txt', 'wb') as f:
f.write(body.decode("utf-8"))