本文整理汇总了Python中scrapy.exporters.XmlItemExporter方法的典型用法代码示例。如果您正苦于以下问题:Python exporters.XmlItemExporter方法的具体用法?Python exporters.XmlItemExporter怎么用?Python exporters.XmlItemExporter使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scrapy.exporters
的用法示例。
在下文中一共展示了exporters.XmlItemExporter方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: spider_opened
# 需要导入模块: from scrapy import exporters [as 别名]
# 或者: from scrapy.exporters import XmlItemExporter [as 别名]
def spider_opened(self, spider):
file_xml = open('%s_items.xml' % spider.name, 'w+b')
self.files[spider] = file_xml
self.exporter = XmlItemExporter(file_xml)
self.exporter.start_exporting()
示例2: process_item
# 需要导入模块: from scrapy import exporters [as 别名]
# 或者: from scrapy.exporters import XmlItemExporter [as 别名]
def process_item(self, item, spider):
"""
Process each item to export them according to their year.
"""
if not spider.get_body:
return item
# Get the year
time_format = "%Y-%m-%d %H:%M:%S%z"
timestamp = datetime.strptime(item['timestampReceived'], time_format)
year = str(timestamp.year)
filename = self.file_base + year + 'Bodies.xml'
if year in self.exporters:
# If we already have an exporter for this year, select it
exporter = self.exporters[year]['exporter']
else:
# If we encounter this year for the first time, then:
# Open a new file
dest_file = open(filename, 'wb')
# Store file and create exporter in dictionary
self.exporters[year] = {
'file': dest_file,
'exporter': XmlItemExporter(dest_file)
}
# Initialize the exporter
exporter = self.exporters[year]['exporter']
exporter.root_element = 'emails'
exporter.item_element = 'email'
exporter.fields_to_export = self.fields_to_export
exporter.start_exporting()
# Now we can export the item for real
exporter.export_item(item)
return item
示例3: spider_opened
# 需要导入模块: from scrapy import exporters [as 别名]
# 或者: from scrapy.exporters import XmlItemExporter [as 别名]
def spider_opened(self, spider):
file = open('pydata_items.xml', 'w+b')
self.files[spider] = file
self.exporter = XmlItemExporter(file)
self.exporter.start_exporting()
示例4: spider_opened
# 需要导入模块: from scrapy import exporters [as 别名]
# 或者: from scrapy.exporters import XmlItemExporter [as 别名]
def spider_opened(self, spider):
file = open('europython_items.xml', 'w+b')
self.files[spider] = file
self.exporter = XmlItemExporter(file)
self.exporter.start_exporting()