笨鸟编程-零基础入门Pyhton教程

 找回密码
 立即注册

条目导出器

发布者: 笨鸟自学网



PythonItemExporter

XmlItemExporter

classscrapy.exporters.XmlItemExporter(fileitem_element='item'root_element='items'**kwargs)

将XML格式的项导出到指定的文件对象。

参数
  • file -- 用于导出数据的类似文件的对象。它的 write 方法应接受 bytes (以二进制模式打开的磁盘文件, io.BytesIO 物体等)

  • root_element (str) -- 导出的XML中根元素的名称。

  • item_element (str) -- 导出的XML中每个item元素的名称。

此的其他关键字参数 __init__ 方法传递给 BaseItemExporter __init__ 方法。

该出口商的典型产出为:

<?xml version="1.0" encoding="utf-8"?>
<items>
  <item>
    <name>Color TV</name>
    <price>1200</price>
 </item>
  <item>
    <name>DVD player</name>
    <price>200</price>
 </item>
</items>

除非在 serialize_field() 方法,多值字段通过序列化 <value> 元素。这是为了方便起见,因为多值字段非常常见。

例如,项目:

Item(name=['John', 'Doe'], age='23')

将序列化为:

<?xml version="1.0" encoding="utf-8"?>
<items>
  <item>
    <name>
      <value>John</value>
      <value>Doe</value>
    </name>
    <age>23</age>
  </item>
</items>

CsvItemExporter

classscrapy.exporters.CsvItemExporter(fileinclude_headers_line=Truejoin_multivalued=','errors=None**kwargs)

将CSV格式的项目导出到给定的类似文件的对象。如果 fields_to_export 属性已设置,将用于定义csv列及其顺序。这个 export_empty_fields 属性对此导出程序没有影响。

参数
  • file -- 用于导出数据的类似文件的对象。它的 write 方法应接受 bytes (以二进制模式打开的磁盘文件, io.BytesIO 物体等)

  • include_headers_line (str) -- 如果启用,则使导出器输出一个标题行,其中字段名取自 BaseItemExporter.fields_to_export 或第一个导出项字段。

  • join_multivalued -- 如果找到,将用于联接多值字段的字符。

  • errors (str) -- 指定如何处理编码和解码错误的可选字符串。有关详细信息,请参阅 io.TextIOWrapper .

此的其他关键字参数 __init__ 方法传递给 BaseItemExporter __init__ 方法,以及对 csv.writer() 函数,因此您可以使用任何 csv.writer() 自定义此导出器的函数参数。

该出口商的典型产出为:

product,price
Color TV,1200
DVD player,200 

上一篇:调度程序

Archiver|手机版|笨鸟自学网 ( 粤ICP备20019910号 )

GMT+8, 2024-9-8 09:37 , Processed in 0.019297 second(s), 17 queries .

© 2001-2020

返回顶部