本文整理匯總了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()