Scrapy – 项目
剪贴的主要目标是从非结构化源(通常是网页)中提取结构化数据。 Spiders 将提取的数据返回为 items ,定义键-值对的Python对象。
支点 multiple types of items . 创建项目时,可以使用所需的任何类型的项目。当您编写接收项目的程式码时,您的程式码应该 work for any item type .
项目类型¶
Scrapy支持以下类型的项目,通过 itemadapter 类库: dictionaries , Item objects , dataclass objects 和 attrs objects .
辞典¶
作为项目类型, dict 方便又熟悉。
项目对象¶
Item 提供了一个 dict -像API加上其他功能,使其成为功能最齐全的项目类型:
-
class
scrapy.item.Item([arg])[源代码]¶ -
Item对象复制标准dictAPI,包括其__init__方法。Item允许定义字段名,以便:-
KeyError在使用未定义的字段名时引发(即防止打字错误被忽略) -
Item exporters 默认情况下可以导出所有字段,即使第一个刮取的对象没有所有字段的值
Item还允许定义字段元数据,可用于 customize serialization .trackref轨道Item对象来帮助查找内存泄漏(请参见 使用调试内存泄漏 trackref )Item对象还提供以下附加API成员:-
copy()¶
-
deepcopy()¶ -
返回A
deepcopy()这个项目的。
-
fields¶ -
包含 所有已声明的字段 对于这个项目,不仅仅是那些填充的。键是字段名,值是
Field中使用的对象 Item declaration .
-
例子::
from scrapy.item import Item, Field class CustomItem(Item): one_field = Field() another_field = Field()
数据类对象¶
2.2 新版功能.
dataclass() 允许使用字段名定义项类,以便 item exporters 默认情况下可以导出所有字段,即使第一个刮取的对象没有所有字段的值。
此外, dataclass 项目还允许:
-
定义每个定义字段的类型和默认值。
-
通过定义自定义字段元数据
dataclasses.field(),可以用来 customize serialization .
它们在python3.7或更高版本中以本机方式工作,或者使用 dataclasses backport 在Python 3.6中。
例子::
from dataclasses import dataclass @dataclass class CustomItem: one_field: str another_field: int
注解
在运行时不强制使用字段类型。
属性对象¶
2.2 新版功能.
attr.s() 允许使用字段名定义项类,以便 item exporters 默认情况下可以导出所有字段,即使第一个刮取的对象没有所有字段的值。
此外, attr.s 项目还允许:
-
定义每个定义字段的类型和默认值。
-
定义自定义字段 metadata ,可以用来 customize serialization .
为了使用此类型 attrs package 需要安装。
例子::
import attr @attr.s class CustomItem: one_field = attr.ib() another_field = attr.ib()
使用项目对象¶
声明项子类¶
项子类使用简单的类定义语法和 Field 物体。下面是一个例子:
import scrapy class Product(scrapy.Item): name = scrapy.Field() price = scrapy.Field() stock = scrapy.Field() tags = scrapy.Field() last_updated = scrapy.Field(serializer=str)
注解
那些熟悉 Django 会注意到报废物品的声明类似于 Django Models 但是,由于不存在不同字段类型的概念,因此片段项要简单得多。
声明字段¶
Field 对象用于为每个字段指定元数据。例如,用于 last_updated 上面示例中所示的字段。
可以为每个字段指定任何类型的元数据。对接受的值没有限制 Field 物体。出于同样的原因,没有所有可用元数据键的引用列表。中定义的每个键 Field 对象可以由不同的组件使用,只有那些组件知道它。您还可以定义和使用任何其他 Field 为了你自己的需要,也要输入你的项目。的主要目标 Field 对象是提供一种在一个地方定义所有字段元数据的方法。通常,行为依赖于每个字段的组件使用特定的字段键来配置该行为。您必须参考它们的文档来查看每个组件使用的元数据键。
重要的是要注意 Field 用于声明该项的对象不会保留分配为类属性的状态。相反,可以通过 Item.fields 属性。
-
class
scrapy.item.Field([arg])[源代码]¶ -
这个
Field类只是内置的别名dict类,不提供任何额外的功能或属性。换言之,Field对象是普通的旧python dict。单独的类用于支持 item declaration syntax 基于类属性。
注解
也可以为声明字段元数据 dataclass 和 attrs 项目。请参考文件 dataclasses.field 和 attr.ib 更多信息。
使用项目对象¶
下面是一些使用项执行的常见任务的示例,使用 Product 项目 declared above . 您会注意到API与 dict 应用程序编程接口。
创建项目¶
>>> product = Product(name='Desktop PC', price=1000) >>> print(product) Product(name='Desktop PC', price=1000)
获取字段值¶
>>> product['name'] Desktop PC >>> product.get('name') Desktop PC
>>> product['price'] 1000
>>> product['last_updated'] Traceback (most recent call last): ... KeyError: 'last_updated'
>>> product.get('last_updated', 'not set') not set
>>> product['lala'] # getting unknown field Traceback (most recent call last): ... KeyError: 'lala'
>>> product.get('lala', 'unknown field') 'unknown field'
>>> 'name' in product # is name field populated? True
>>> 'last_updated' in product # is last_updated populated? False
>>> 'last_updated' in product.fields # is last_updated a declared field? True
>>> 'lala' in product.fields # is lala a declared field? False
设置字段值¶
>>> product['last_updated'] = 'today' >>> product['last_updated'] today
>>> product['lala'] = 'test' # setting unknown field Traceback (most recent call last): ... KeyError: 'Product does not support field: lala'
访问所有填充的值¶
要访问所有填充的值,只需使用 dict 应用程序编程接口:
>>> product.keys() ['price', 'name']
>>> product.items() [('price', 1000), ('name', 'Desktop PC')]
复制项目¶
要复制项目,必须首先决定是要浅副本还是深副本。
如果您的物品包含 mutable 值如列表或字典,一个浅拷贝将在所有不同的拷贝中保持对相同可变值的引用。
例如,如果您有一个带有标记列表的项目,并且您创建了该项目的浅副本,那么原始项目和副本都具有相同的标记列表。向其中一个项目的列表中添加标记也会将标记添加到另一个项目中。
如果这不是所需的行为,请使用深度复制。
见 copy 更多信息。
要创建项目的浅副本,可以调用 copy() 在现有项上 (product2 = product.copy() )或从现有项实例化项类 (product2 = Product(product) )
要创建深度复制,请调用 deepcopy() 相反 (product2 = product.deepcopy() )
其他常见任务¶
从项目创建dict:
>>> dict(product) # create a dict from all populated values {'price': 1000, 'name': 'Desktop PC'}
从dicts创建项目:
>>> Product({'name': 'Laptop PC', 'price': 1500}) Product(price=1500, name='Laptop PC')
>>> Product({'name': 'Laptop PC', 'lala': 1500}) # warning: unknown field in dict Traceback (most recent call last): ... KeyError: 'Product does not support field: lala'
扩展项子类¶
您可以通过声明原始项的子类来扩展项(添加更多字段或更改某些字段的元数据)。
例如::
class DiscountedProduct(Product): discount_percent = scrapy.Field(serializer=str) discount_expiration_date = scrapy.Field()
您还可以通过使用前面的字段元数据并附加更多值或更改现有值来扩展字段元数据,如:
class SpecificProduct(Product): name = scrapy.Field(Product.fields['name'], serializer=my_serializer)
添加(或替换)了 serializer 的元数据键 name 字段,保留所有以前存在的元数据值。
支持所有项目类型¶
在接收项的代码中,例如 item pipelines 或 spider middlewares ,使用 ItemAdapter 类与 is_item() 函数来编写适用于任何 supported item type :
与项目相关的其他类别¶
-
class
scrapy.item.ItemMeta(class_name, bases, attrs)[源代码]¶ -
Metaclass 属于
Item它处理字段定义。
项目类型¶
Scrapy支持以下类型的项目,通过 itemadapter 类库: dictionaries , Item objects , dataclass objects 和 attrs objects .
辞典¶
作为项目类型, dict 方便又熟悉。
项目对象¶
Item 提供了一个 dict -像API加上其他功能,使其成为功能最齐全的项目类型:
-
class
scrapy.item.Item([arg])[源代码]¶ -
Item对象复制标准dictAPI,包括其__init__方法。Item允许定义字段名,以便:-
KeyError在使用未定义的字段名时引发(即防止打字错误被忽略) -
Item exporters 默认情况下可以导出所有字段,即使第一个刮取的对象没有所有字段的值
Item还允许定义字段元数据,可用于 customize serialization .trackref轨道Item对象来帮助查找内存泄漏(请参见 使用调试内存泄漏 trackref )Item对象还提供以下附加API成员:-
copy()¶
-
deepcopy()¶ -
返回A
deepcopy()这个项目的。
-
fields¶ -
包含 所有已声明的字段 对于这个项目,不仅仅是那些填充的。键是字段名,值是
Field中使用的对象 Item declaration .
-
例子::
from scrapy.item import Item, Field class CustomItem(Item): one_field = Field() another_field = Field()
数据类对象¶
2.2 新版功能.
dataclass() 允许使用字段名定义项类,以便 item exporters 默认情况下可以导出所有字段,即使第一个刮取的对象没有所有字段的值。
此外, dataclass 项目还允许:
-
定义每个定义字段的类型和默认值。
-
通过定义自定义字段元数据
dataclasses.field(),可以用来 customize serialization .
它们在python3.7或更高版本中以本机方式工作,或者使用 dataclasses backport 在Python 3.6中。
例子::
from dataclasses import dataclass @dataclass class CustomItem: one_field: str another_field: int
注解
在运行时不强制使用字段类型。
属性对象¶
2.2 新版功能.
attr.s() 允许使用字段名定义项类,以便 item exporters 默认情况下可以导出所有字段,即使第一个刮取的对象没有所有字段的值。
此外, attr.s 项目还允许:
-
定义每个定义字段的类型和默认值。
-
定义自定义字段 metadata ,可以用来 customize serialization .
为了使用此类型 attrs package 需要安装。
例子::
import attr @attr.s class CustomItem: one_field = attr.ib() another_field = attr.ib()
辞典¶
作为项目类型, dict 方便又熟悉。
项目对象¶
Item 提供了一个 dict -像API加上其他功能,使其成为功能最齐全的项目类型:
-
class
scrapy.item.Item([arg])[源代码]¶ -
Item对象复制标准dictAPI,包括其__init__方法。Item允许定义字段名,以便:-
KeyError在使用未定义的字段名时引发(即防止打字错误被忽略) -
Item exporters 默认情况下可以导出所有字段,即使第一个刮取的对象没有所有字段的值
Item还允许定义字段元数据,可用于 customize serialization .trackref轨道Item对象来帮助查找内存泄漏(请参见 使用调试内存泄漏 trackref )Item对象还提供以下附加API成员:-
copy()¶
-
deepcopy()¶ -
返回A
deepcopy()这个项目的。
-
fields¶ -
包含 所有已声明的字段 对于这个项目,不仅仅是那些填充的。键是字段名,值是
Field中使用的对象 Item declaration .
-
例子::
from scrapy.item import Item, Field class CustomItem(Item): one_field = Field() another_field = Field()
数据类对象¶
2.2 新版功能.
dataclass() 允许使用字段名定义项类,以便 item exporters 默认情况下可以导出所有字段,即使第一个刮取的对象没有所有字段的值。
此外, dataclass 项目还允许:
-
定义每个定义字段的类型和默认值。
-
通过定义自定义字段元数据
dataclasses.field(),可以用来 customize serialization .
它们在python3.7或更高版本中以本机方式工作,或者使用 dataclasses backport 在Python 3.6中。
例子::
from dataclasses import dataclass @dataclass class CustomItem: one_field: str another_field: int
注解
在运行时不强制使用字段类型。
属性对象¶
2.2 新版功能.
attr.s() 允许使用字段名定义项类,以便 item exporters 默认情况下可以导出所有字段,即使第一个刮取的对象没有所有字段的值。
此外, attr.s 项目还允许:
-
定义每个定义字段的类型和默认值。
-
定义自定义字段 metadata ,可以用来 customize serialization .
为了使用此类型 attrs package 需要安装。
例子::
import attr @attr.s class CustomItem: one_field = attr.ib() another_field = attr.ib()
使用项目对象¶
声明项子类¶
项子类使用简单的类定义语法和 Field 物体。下面是一个例子:
import scrapy class Product(scrapy.Item): name = scrapy.Field() price = scrapy.Field() stock = scrapy.Field() tags = scrapy.Field() last_updated = scrapy.Field(serializer=str)
注解
那些熟悉 Django 会注意到报废物品的声明类似于 Django Models 但是,由于不存在不同字段类型的概念,因此片段项要简单得多。
声明字段¶
Field 对象用于为每个字段指定元数据。例如,用于 last_updated 上面示例中所示的字段。
可以为每个字段指定任何类型的元数据。对接受的值没有限制 Field 物体。出于同样的原因,没有所有可用元数据键的引用列表。中定义的每个键 Field 对象可以由不同的组件使用,只有那些组件知道它。您还可以定义和使用任何其他 Field 为了你自己的需要,也要输入你的项目。的主要目标 Field 对象是提供一种在一个地方定义所有字段元数据的方法。通常,行为依赖于每个字段的组件使用特定的字段键来配置该行为。您必须参考它们的文档来查看每个组件使用的元数据键。
重要的是要注意 Field 用于声明该项的对象不会保留分配为类属性的状态。相反,可以通过 Item.fields 属性。
-
class
scrapy.item.Field([arg])[源代码]¶ -
这个
Field类只是内置的别名dict类,不提供任何额外的功能或属性。换言之,Field对象是普通的旧python dict。单独的类用于支持 item declaration syntax 基于类属性。
注解
也可以为声明字段元数据 dataclass 和 attrs 项目。请参考文件 dataclasses.field 和 attr.ib 更多信息。
使用项目对象¶
下面是一些使用项执行的常见任务的示例,使用 Product 项目 declared above . 您会注意到API与 dict 应用程序编程接口。
创建项目¶
>>> product = Product(name='Desktop PC', price=1000) >>> print(product) Product(name='Desktop PC', price=1000)
获取字段值¶
>>> product['name'] Desktop PC >>> product.get('name') Desktop PC
>>> product['price'] 1000
>>> product['last_updated'] Traceback (most recent call last): ... KeyError: 'last_updated'
>>> product.get('last_updated', 'not set') not set
>>> product['lala'] # getting unknown field Traceback (most recent call last): ... KeyError: 'lala'
>>> product.get('lala', 'unknown field') 'unknown field'
>>> 'name' in product # is name field populated? True
>>> 'last_updated' in product # is last_updated populated? False
>>> 'last_updated' in product.fields # is last_updated a declared field? True
>>> 'lala' in product.fields # is lala a declared field? False
设置字段值¶
>>> product['last_updated'] = 'today' >>> product['last_updated'] today
>>> product['lala'] = 'test' # setting unknown field Traceback (most recent call last): ... KeyError: 'Product does not support field: lala'
访问所有填充的值¶
要访问所有填充的值,只需使用 dict 应用程序编程接口:
>>> product.keys() ['price', 'name']
>>> product.items() [('price', 1000), ('name', 'Desktop PC')]
复制项目¶
要复制项目,必须首先决定是要浅副本还是深副本。
如果您的物品包含 mutable 值如列表或字典,一个浅拷贝将在所有不同的拷贝中保持对相同可变值的引用。
例如,如果您有一个带有标记列表的项目,并且您创建了该项目的浅副本,那么原始项目和副本都具有相同的标记列表。向其中一个项目的列表中添加标记也会将标记添加到另一个项目中。
如果这不是所需的行为,请使用深度复制。
见 copy 更多信息。
要创建项目的浅副本,可以调用 copy() 在现有项上 (product2 = product.copy() )或从现有项实例化项类 (product2 = Product(product) )
要创建深度复制,请调用 deepcopy() 相反 (product2 = product.deepcopy() )
其他常见任务¶
从项目创建dict:
>>> dict(product) # create a dict from all populated values {'price': 1000, 'name': 'Desktop PC'}
从dicts创建项目:
>>> Product({'name': 'Laptop PC', 'price': 1500}) Product(price=1500, name='Laptop PC')
>>> Product({'name': 'Laptop PC', 'lala': 1500}) # warning: unknown field in dict Traceback (most recent call last): ... KeyError: 'Product does not support field: lala'
扩展项子类¶
您可以通过声明原始项的子类来扩展项(添加更多字段或更改某些字段的元数据)。
例如::
class DiscountedProduct(Product): discount_percent = scrapy.Field(serializer=str) discount_expiration_date = scrapy.Field()
您还可以通过使用前面的字段元数据并附加更多值或更改现有值来扩展字段元数据,如:
class SpecificProduct(Product): name = scrapy.Field(Product.fields['name'], serializer=my_serializer)
添加(或替换)了 serializer 的元数据键 name 字段,保留所有以前存在的元数据值。
声明项子类¶
项子类使用简单的类定义语法和 Field 物体。下面是一个例子:
import scrapy class Product(scrapy.Item): name = scrapy.Field() price = scrapy.Field() stock = scrapy.Field() tags = scrapy.Field() last_updated = scrapy.Field(serializer=str)
注解
那些熟悉 Django 会注意到报废物品的声明类似于 Django Models 但是,由于不存在不同字段类型的概念,因此片段项要简单得多。
声明字段¶
Field 对象用于为每个字段指定元数据。例如,用于 last_updated 上面示例中所示的字段。
可以为每个字段指定任何类型的元数据。对接受的值没有限制 Field 物体。出于同样的原因,没有所有可用元数据键的引用列表。中定义的每个键 Field 对象可以由不同的组件使用,只有那些组件知道它。您还可以定义和使用任何其他 Field 为了你自己的需要,也要输入你的项目。的主要目标 Field 对象是提供一种在一个地方定义所有字段元数据的方法。通常,行为依赖于每个字段的组件使用特定的字段键来配置该行为。您必须参考它们的文档来查看每个组件使用的元数据键。
重要的是要注意 Field 用于声明该项的对象不会保留分配为类属性的状态。相反,可以通过 Item.fields 属性。
-
class
scrapy.item.Field([arg])[源代码]¶ -
这个
Field类只是内置的别名dict类,不提供任何额外的功能或属性。换言之,Field对象是普通的旧python dict。单独的类用于支持 item declaration syntax 基于类属性。
注解
也可以为声明字段元数据 dataclass 和 attrs 项目。请参考文件 dataclasses.field 和 attr.ib 更多信息。
使用项目对象¶
下面是一些使用项执行的常见任务的示例,使用 Product 项目 declared above . 您会注意到API与 dict 应用程序编程接口。
创建项目¶
>>> product = Product(name='Desktop PC', price=1000) >>> print(product) Product(name='Desktop PC', price=1000)
获取字段值¶
>>> product['name'] Desktop PC >>> product.get('name') Desktop PC
>>> product['price'] 1000
>>> product['last_updated'] Traceback (most recent call last): ... KeyError: 'last_updated'
>>> product.get('last_updated', 'not set') not set
>>> product['lala'] # getting unknown field Traceback (most recent call last): ... KeyError: 'lala'
>>> product.get('lala', 'unknown field') 'unknown field'
>>> 'name' in product # is name field populated? True
>>> 'last_updated' in product # is last_updated populated? False
>>> 'last_updated' in product.fields # is last_updated a declared field? True
>>> 'lala' in product.fields # is lala a declared field? False
设置字段值¶
>>> product['last_updated'] = 'today' >>> product['last_updated'] today
>>> product['lala'] = 'test' # setting unknown field Traceback (most recent call last): ... KeyError: 'Product does not support field: lala'
访问所有填充的值¶
要访问所有填充的值,只需使用 dict 应用程序编程接口:
>>> product.keys() ['price', 'name']
>>> product.items() [('price', 1000), ('name', 'Desktop PC')]
复制项目¶
要复制项目,必须首先决定是要浅副本还是深副本。
如果您的物品包含 mutable 值如列表或字典,一个浅拷贝将在所有不同的拷贝中保持对相同可变值的引用。
例如,如果您有一个带有标记列表的项目,并且您创建了该项目的浅副本,那么原始项目和副本都具有相同的标记列表。向其中一个项目的列表中添加标记也会将标记添加到另一个项目中。
如果这不是所需的行为,请使用深度复制。
见 copy 更多信息。
要创建项目的浅副本,可以调用 copy() 在现有项上 (product2 = product.copy() )或从现有项实例化项类 (product2 = Product(product) )
要创建深度复制,请调用 deepcopy() 相反 (product2 = product.deepcopy() )
其他常见任务¶
从项目创建dict:
>>> dict(product) # create a dict from all populated values {'price': 1000, 'name': 'Desktop PC'}
从dicts创建项目:
>>> Product({'name': 'Laptop PC', 'price': 1500}) Product(price=1500, name='Laptop PC')
>>> Product({'name': 'Laptop PC', 'lala': 1500}) # warning: unknown field in dict Traceback (most recent call last): ... KeyError: 'Product does not support field: lala'
创建项目¶
>>> product = Product(name='Desktop PC', price=1000) >>> print(product) Product(name='Desktop PC', price=1000)
获取字段值¶
>>> product['name'] Desktop PC >>> product.get('name') Desktop PC
>>> product['price'] 1000
>>> product['last_updated'] Traceback (most recent call last): ... KeyError: 'last_updated'
>>> product.get('last_updated', 'not set') not set
>>> product['lala'] # getting unknown field Traceback (most recent call last): ... KeyError: 'lala'
>>> product.get('lala', 'unknown field') 'unknown field'
>>> 'name' in product # is name field populated? True
>>> 'last_updated' in product # is last_updated populated? False
>>> 'last_updated' in product.fields # is last_updated a declared field? True
>>> 'lala' in product.fields # is lala a declared field? False
设置字段值¶
>>> product['last_updated'] = 'today' >>> product['last_updated'] today
>>> product['lala'] = 'test' # setting unknown field Traceback (most recent call last): ... KeyError: 'Product does not support field: lala'
访问所有填充的值¶
要访问所有填充的值,只需使用 dict 应用程序编程接口:
>>> product.keys() ['price', 'name']
>>> product.items() [('price', 1000), ('name', 'Desktop PC')]
复制项目¶
要复制项目,必须首先决定是要浅副本还是深副本。
如果您的物品包含 mutable 值如列表或字典,一个浅拷贝将在所有不同的拷贝中保持对相同可变值的引用。
例如,如果您有一个带有标记列表的项目,并且您创建了该项目的浅副本,那么原始项目和副本都具有相同的标记列表。向其中一个项目的列表中添加标记也会将标记添加到另一个项目中。
如果这不是所需的行为,请使用深度复制。
见 copy 更多信息。
要创建项目的浅副本,可以调用 copy() 在现有项上 (product2 = product.copy() )或从现有项实例化项类 (product2 = Product(product) )
要创建深度复制,请调用 deepcopy() 相反 (product2 = product.deepcopy() )
其他常见任务¶
从项目创建dict:
>>> dict(product) # create a dict from all populated values {'price': 1000, 'name': 'Desktop PC'}
从dicts创建项目:
>>> Product({'name': 'Laptop PC', 'price': 1500}) Product(price=1500, name='Laptop PC')
>>> Product({'name': 'Laptop PC', 'lala': 1500}) # warning: unknown field in dict Traceback (most recent call last): ... KeyError: 'Product does not support field: lala'
扩展项子类¶
您可以通过声明原始项的子类来扩展项(添加更多字段或更改某些字段的元数据)。
例如::
class DiscountedProduct(Product): discount_percent = scrapy.Field(serializer=str) discount_expiration_date = scrapy.Field()
您还可以通过使用前面的字段元数据并附加更多值或更改现有值来扩展字段元数据,如:
class SpecificProduct(Product): name = scrapy.Field(Product.fields['name'], serializer=my_serializer)
添加(或替换)了 serializer 的元数据键 name 字段,保留所有以前存在的元数据值。
支持所有项目类型¶
在接收项的代码中,例如 item pipelines 或 spider middlewares ,使用 ItemAdapter 类与 is_item() 函数来编写适用于任何 supported item type :
与项目相关的其他类别¶
-
class
scrapy.item.ItemMeta(class_name, bases, attrs)[源代码]¶ -
Metaclass 属于
Item它处理字段定义。