アイテムエクスポーター¶
アイテムを集めたら, そのアイテムを永続化またはエクスポートして, 他のアプリケーションでデータを使用することがしばしばあります. つまり, 結局のところ, スクレイピング工程の全目的です.
この目的のため, Scrapy は, XML, CSV, JSON などのさまざまな出力形式に対しアイテムエクスポーターのコレクションを提供します.
アイテムエクスポーターを使う¶
あなたが急いでいて, アイテムエクスポータを使用してスクレイピングしたデータを出力したい場合は, フィードのエクスポート を参照してください. そうではなく, アイテムエクスポータがどのように機能するかを知りたい, もしくは (デフォルトのエクスポートでカバーされていない)カスタム機能をもっと必要とする場合は以下をお読みください.
アイテムエクスポーターを使用するには, 必要な引数でインスタンス化する必要があります. 各エクスポーターは異なる引数を必要とするため, in ビルトインアイテムエクスポーターリファレンス を参照してください. エクスポーターのインスタンスを作成した後は, 以下の作業を行う必要があります:
- エクスポートプロセスの開始を知らせるために
start_exporting()
メソッドを呼び出します. - エクスポートする各アイテムの
export_item()
メソッドを呼び出します. - 最後に
finish_exporting()
メソッドを呼び出してエクスポートプロセスの終了を知らせます.
ここで アイテムパイプライン を使用して, スクレイピングしたアイテムを異なるファイルにエクスポートするアイテムパイプライン(スパイダーごとに1つ)の例を紹介します:
from scrapy import signals
from scrapy.exporters import XmlItemExporter
class XmlExportPipeline(object):
def __init__(self):
self.files = {}
@classmethod
def from_crawler(cls, crawler):
pipeline = cls()
crawler.signals.connect(pipeline.spider_opened, signals.spider_opened)
crawler.signals.connect(pipeline.spider_closed, signals.spider_closed)
return pipeline
def spider_opened(self, spider):
file = open('%s_products.xml' % spider.name, 'w+b')
self.files[spider] = file
self.exporter = XmlItemExporter(file)
self.exporter.start_exporting()
def spider_closed(self, spider):
self.exporter.finish_exporting()
file = self.files.pop(spider)
file.close()
def process_item(self, item, spider):
self.exporter.export_item(item)
return item
アイテムフィールドのシリアル化¶
デフォルトでは, フィールド値は未変更のシリアル化ライブラリに渡され, シリアル化する方法の決定権は各シリアル化ライブラリに委譲されます.
ただし, 各フィールドの値がシリアライゼーションライブラリに渡される前に, シリアライズされる方法をカスタマイズできます.
フィールドのシリアル化方法をカスタマイズするには, 次の2つの方法があります.
1. フィールド内のシリアライザの宣言¶
Item
を使用する場合,
フィールドメタデータ でシリアライザを宣言できます.
シリアライザは, 呼び出し可能な値を受け取ってシリアル化された値を返す形式でなければなりません.
例:
import scrapy
def serialize_price(value):
return '$ %s' % str(value)
class Product(scrapy.Item):
name = scrapy.Field()
price = scrapy.Field(serializer=serialize_price)
2. serialize_field() メソッドのオーバーライド¶
serialize_field()
メソッドをオーバーライドして、フィールド値のエクスポート方法をカスタマイズすることもできます.
カスタムコードの後に, 基本クラスの serialize_field()
メソッドを呼び出すようにしてください.
例:
from scrapy.exporter import XmlItemExporter
class ProductXmlExporter(XmlItemExporter):
def serialize_field(self, field, name, value):
if field == 'price':
return '$ %s' % str(value)
return super(Product, self).serialize_field(field, name, value)
ビルトインアイテムエクスポーターリファレンス¶
以下に, Scrapy にバンドルされているアイテムエクスポータのリストがあります. これらの中には, 2つのアイテムをエクスポートしていると仮定した出力例が含まれています:
Item(name='Color TV', price='1200')
Item(name='DVD player', price='200')
BaseItemExporter¶
-
class
scrapy.exporters.
BaseItemExporter
(fields_to_export=None, export_empty_fields=False, encoding='utf-8')¶ これは, すべてのアイテムエクスポータの(抽象)基本クラスです. エクスポートするフィールドの定義, 空のフィールドのエクスポートの指定, 使用するエンコーディングなど, すべての(具体的な)アイテムエクスポータで使用される共通の機能をサポートしています.
これらの機能は,
fields_to_export
,export_empty_fields
,encoding
のそれぞれのインスタンス属性を設定するコンストラクタ引数で設定できます.-
export_item
(item)¶
指定された項目をエクスポートします. このメソッドはサブクラスで実装する必要があります.
-
serialize_field
(field, name, value)¶
指定されたフィールドの直列化された値を返します. 特定のフィールドまたは値のシリアライズ/エクスポートの方法を制御する場合は,
カスタムアイテムエクスポータでこのメソッドをオーバーライドできます.- デフォルトでは、このメソッドは フィールドで宣言された シリアライザを検索し,
- そのシリアライザをその値に適用した結果を返します.
シリアライザが見つからない場合,
encoding
属性で宣言されたエンコーディングを使用して
エンコードされた
str
のunicode
値を除いて, 値は変更されません.param field: フィールドはシリアル化されています。 生の dict がエクスポートされている場合(
Item
ではなく)*フィールド* 値は空の dict です. :type field:Field
オブジェクトまたは空の dictparam name: シリアル化されているフィールドの名前 type name: str :param value: シリアル化された値
-
start_exporting
()¶ - エクスポートプロセスの開始を知らせます.
一部のエクスポーターは, これを使用して必要なヘッダー(たとえば,
XmlItemExporter
)を生成することがあります. アイテムをエクスポートする前に, このメソッドを呼び出す必要があります.-
finish_exporting
()¶ エクスポートプロセスの終了を知らせます.
一部のエクスポーターは, これを使用して必要なフッター (たとえば,
XmlItemExporter
のような)を生成することがあります. エクスポートする項目がなくなったら, 必ずこのメソッドを呼び出す必要があります.-
fields_to_export
¶
エクスポートされるフィールドの名前を持つリスト, またはすべてのフィールドをエクスポートする場合は None です. デフォルトは None です.
一部のエクスポーター (
CsvItemExporter
など) は, この属性で定義されたフィールドの順序を尊重します.いくつかのエクスポーターは, スパイダーが (
Item
インスタンスでない) dictsを返すとき, データを適切にエクスポートするために fields_to_export リストを要求することがあります.-
export_empty_fields
¶ エクスポートされたデータに空のフィールドフィールドまたは空になっていないアイテムフィールドを含めるかどうか.
デフォルトは
False
です. 一部のエクスポーター (CsvItemExporter
など) はこの属性を無視し、空のフィールドを常にエクスポートします. このオプションは dict 項目では無視されます.-
encoding
¶
Unicode値をエンコードするために使用されるエンコード. これはUnicode値にのみ影響します(このエンコーディングを使用してstrに常にシリアル化されます). 他の値の型は, 変更されずに特定の直列化ライブラリに渡されます.
-
-
XmlItemExporter¶
-
class
scrapy.exporters.
XmlItemExporter
(file, item_element='item', root_element='items', **kwargs)¶ Exports Items in XML format to the specified file object.
パラメータ: - file – the file-like object to use for exporting the data.
- root_element (str) – The name of root element in the exported XML.
- item_element (str) – The name of each item element in the exported XML.
The additional keyword arguments of this constructor are passed to the
BaseItemExporter
constructor.A typical output of this exporter would be:
<?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>
Unless overridden in the
serialize_field()
method, multi-valued fields are exported by serializing each value inside a<value>
element. This is for convenience, as multi-valued fields are very common.For example, the item:
Item(name=['John', 'Doe'], age='23')
Would be serialized as:
<?xml version="1.0" encoding="utf-8"?> <items> <item> <name> <value>John</value> <value>Doe</value> </name> <age>23</age> </item> </items>
CsvItemExporter¶
-
class
scrapy.exporters.
CsvItemExporter
(file, include_headers_line=True, join_multivalued=', ', **kwargs)¶ Exports Items in CSV format to the given file-like object. If the
fields_to_export
attribute is set, it will be used to define the CSV columns and their order. Theexport_empty_fields
attribute has no effect on this exporter.パラメータ: - file – the file-like object to use for exporting the data.
- include_headers_line (str) – If enabled, makes the exporter output a header
line with the field names taken from
BaseItemExporter.fields_to_export
or the first exported item fields. - join_multivalued – The char (or chars) that will be used for joining multi-valued fields, if found.
The additional keyword arguments of this constructor are passed to the
BaseItemExporter
constructor, and the leftover arguments to the csv.writer constructor, so you can use any csv.writer constructor argument to customize this exporter.A typical output of this exporter would be:
product,price Color TV,1200 DVD player,200
PickleItemExporter¶
-
class
scrapy.exporters.
PickleItemExporter
(file, protocol=0, **kwargs)¶ Exports Items in pickle format to the given file-like object.
パラメータ: - file – the file-like object to use for exporting the data.
- protocol (int) – The pickle protocol to use.
For more information, refer to the pickle module documentation.
The additional keyword arguments of this constructor are passed to the
BaseItemExporter
constructor.Pickle isn’t a human readable format, so no output examples are provided.
PprintItemExporter¶
-
class
scrapy.exporters.
PprintItemExporter
(file, **kwargs)¶ Exports Items in pretty print format to the specified file object.
パラメータ: file – the file-like object to use for exporting the data. The additional keyword arguments of this constructor are passed to the
BaseItemExporter
constructor.A typical output of this exporter would be:
{'name': 'Color TV', 'price': '1200'} {'name': 'DVD player', 'price': '200'}
Longer lines (when present) are pretty-formatted.
JsonItemExporter¶
-
class
scrapy.exporters.
JsonItemExporter
(file, **kwargs)¶ Exports Items in JSON format to the specified file-like object, writing all objects as a list of objects. The additional constructor arguments are passed to the
BaseItemExporter
constructor, and the leftover arguments to the JSONEncoder constructor, so you can use any JSONEncoder constructor argument to customize this exporter.パラメータ: file – the file-like object to use for exporting the data. A typical output of this exporter would be:
[{"name": "Color TV", "price": "1200"}, {"name": "DVD player", "price": "200"}]
警告
JSON is very simple and flexible serialization format, but it doesn’t scale well for large amounts of data since incremental (aka. stream-mode) parsing is not well supported (if at all) among JSON parsers (on any language), and most of them just parse the entire object in memory. If you want the power and simplicity of JSON with a more stream-friendly format, consider using
JsonLinesItemExporter
instead, or splitting the output in multiple chunks.
JsonLinesItemExporter¶
-
class
scrapy.exporters.
JsonLinesItemExporter
(file, **kwargs)¶ Exports Items in JSON format to the specified file-like object, writing one JSON-encoded item per line. The additional constructor arguments are passed to the
BaseItemExporter
constructor, and the leftover arguments to the JSONEncoder constructor, so you can use any JSONEncoder constructor argument to customize this exporter.パラメータ: file – the file-like object to use for exporting the data. A typical output of this exporter would be:
{"name": "Color TV", "price": "1200"} {"name": "DVD player", "price": "200"}
Unlike the one produced by
JsonItemExporter
, the format produced by this exporter is well suited for serializing large amounts of data.