1

I got this error message in Scrapy spider, when I was writing the html of website to file.

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

98% of the websites html were written correctly, however some threw this error (different file names).To make sure I printed the names and they all exist.

this is the code I use to write to files

#code here
name = response.xpath('//*[@id="itemTitle"]//text()').extract()[1]
with open('{}.html'.format(name), 'wb') as html_file:
    html_file.write(response.body)
hadesfv
  • 386
  • 4
  • 18
  • Gave it a quick google, an answer here (https://stackoverflow.com/questions/31414263/python-using-open-w-filenotfounderror) says a folder may not exist, so check there's no extra slashes being added to the name. – Peter Jun 07 '19 at 16:17
  • @Peter I didn't change any folder path (I haven't even imported os) – hadesfv Jun 07 '19 at 16:21
  • Try opening the file with `wb+` instead of `wb` – marsnebulasoup Jun 07 '19 at 16:26
  • I mean your `response.xpath` could potentially be returning with a slash. I don't really know the syntax though so it's just a guess. – Peter Jun 07 '19 at 16:35
  • @Peter wouldn't have shown in the error message? – hadesfv Jun 07 '19 at 16:48

0 Answers0