写入新fi时找不到文件

2024-09-12 10:30:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Scrapy spider中收到这个错误消息,当时我正在将网站的html写入文件

with open('{}.html'.format(name), 'wb') as html_file:
   FileNotFoundError: [Errno 2] No such file or directory:'CHARCOAL.html'

98%的网站html编写正确,但是有些网站出现了这个错误(不同的文件名)。为了确保我打印了这些名称并且它们都存在

这是我用来写文件的代码

#code here
name = response.xpath('//*[@id="itemTitle"]//text()').extract()[1]
with open('{}.html'.format(name), 'wb') as html_file:
    html_file.write(response.body)

Tags: 文件nameformat消息网站responsehtmlas