Items基本使用

英文文档
items的主要目标是从非结构化的数据中提取结构化的数据。

Item types

scrapy支持的数据类型有: dictionaries, Item objects, dataclass objects, 和 attrs objects.
我们一般都使用dict类型的数据。其他数据类型感兴趣可以在点击我学习。

Item基本使用

import scrapy

class Product(scrapy.Item):
    name = scrapy.Field()
    price = scrapy.Field()
    stock = scrapy.Field()
    tags = scrapy.Field()
    last_updated = scrapy.Field(serializer=str)
# 创建Item
>>> product = Product(name='Desktop PC', price=1000)
>>> print(product)
Product(name='Desktop PC', price=1000)

# 获取field的值
>>> 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'

# 访问所有值
>>> product.keys()
['price', 'name']
>>> product.items()
[('price', 1000), ('name', 'Desktop PC')]

# 复制items
"""
1.浅复制是创建一个新的变量名来连接到同一块内存。
2.深度复制是指在内存中重新开一块区域来存放数据。
"""
product2 = product.copy()  # 浅复制
product2 = Product(product)  # 浅复制
product2 = product.deepcopy()  # 深度复制

# 将字典转换为Items会失败
>>> 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'

# 将items转换为字典
>>> dict(product) # create a dict from all populated values
{
     'price': 1000, 'name': 'Desktop PC'}

你可能感兴趣的:(scrapy学习,python)