Scrapy, a fast high-level web crawling & scraping framework for Python.
Close files in the PerYearXmlExportPipeline documentation example
Adrián Chavesf689615e8d61
diff --git a/docs/topics/exporters.rst b/docs/topics/exporters.rstindex 0a0a1765a..8648daded 100644--- a/docs/topics/exporters.rst+++ b/docs/topics/exporters.rst@@ -50,18 +50,19 @@ value of one of their fields::self.year_to_exporter = {}def close_spider(self, spider):- for exporter in self.year_to_exporter.values():+ for exporter, xml_file in self.year_to_exporter.values():exporter.finish_exporting()+ xml_file.close()def _exporter_for_item(self, item):adapter = ItemAdapter(item)year = adapter['year']if year not in self.year_to_exporter:- f = open(f'{year}.xml', 'wb')- exporter = XmlItemExporter(f)+ xml_file = open(f'{year}.xml', 'wb')+ exporter = XmlItemExporter(xml_file)exporter.start_exporting()- self.year_to_exporter[year] = exporter- return self.year_to_exporter[year]+ self.year_to_exporter[year] = (exporter, xml_file)+ return self.year_to_exporter[year][0]def process_item(self, item, spider):exporter = self._exporter_for_item(item)