Python版本管理:pyenv和pyenv-virtualenv
Scrapy爬蟲入門教程一 安裝和基本使用
Scrapy爬蟲入門教程二 官方提供Demo
Scrapy爬蟲入門教程三 命令行工具介紹和示例
Scrapy爬蟲入門教程四 Spider(爬蟲)
Scrapy爬蟲入門教程五 Selectors(選擇器)
Scrapy爬蟲入門教程六 Items(項(xiàng)目)
Scrapy爬蟲入門教程七 Item Loaders(項(xiàng)目加載器)
Scrapy爬蟲入門教程八 交互式 shell 方便調(diào)試
Scrapy爬蟲入門教程九 Item Pipeline(項(xiàng)目管道)
Scrapy爬蟲入門教程十 Feed exports(導(dǎo)出文件)
Scrapy爬蟲入門教程十一 Request和Response(請(qǐng)求和響應(yīng))
Scrapy爬蟲入門教程十二 Link Extractors(鏈接提取器)
開發(fā)環(huán)境:
Python 3.6.0 版本
(當(dāng)前最新)
Scrapy 1.3.2 版本
(當(dāng)前最新)
Items
主要目標(biāo)是從非結(jié)構(gòu)化來源(通常是網(wǎng)頁)提取結(jié)構(gòu)化數(shù)據(jù)。Scrapy爬蟲可以將提取的數(shù)據(jù)作為Python語句返回意推。雖然方便和熟悉堕虹,Python dicts缺乏結(jié)構(gòu):很容易在字段名稱中輸入錯(cuò)誤或返回不一致的數(shù)據(jù)扛邑,特別是在與許多爬蟲的大項(xiàng)目刻盐。
要定義公共輸出數(shù)據(jù)格式,Scrapy提供Item類舍悯。 Item對(duì)象是用于收集所抓取的數(shù)據(jù)的簡單容器术浪。它們提供了一個(gè)類似字典的 API,具有用于聲明其可用字段的方便的語法憋肖。
各種Scrapy組件使用項(xiàng)目提供的額外信息:導(dǎo)出器查看聲明的字段以計(jì)算要導(dǎo)出的列因痛,序列化可以使用項(xiàng)字段元數(shù)據(jù)trackref 定制,跟蹤項(xiàng)實(shí)例以幫助查找內(nèi)存泄漏(請(qǐng)參閱使用trackref調(diào)試內(nèi)存泄漏)等。
聲明項(xiàng)目
使用簡單的類定義語法和Field 對(duì)象來聲明項(xiàng)目。這里是一個(gè)例子:
import scrapy
class Product(scrapy.Item):
name = scrapy.Field()
price = scrapy.Field()
stock = scrapy.Field()
last_updated = scrapy.Field(serializer=str)
注意
熟悉Django的人會(huì)注意到Scrapy Items被聲明為類似于Django Models霎褐,只是Scrapy Items比較簡單,因?yàn)闆]有不同字段類型的概念谭企。
項(xiàng)目字段
Field
對(duì)象用于為每個(gè)字段指定元數(shù)據(jù)。例如评肆,last_updated
上面示例中所示的字段的序列化函數(shù)债查。
您可以為每個(gè)字段指定任何種類的元數(shù)據(jù)。對(duì)Field對(duì)象接受的值沒有限制瓜挽。出于同樣的原因攀操,沒有所有可用元數(shù)據(jù)鍵的參考列表。Field對(duì)象中定義的每個(gè)鍵可以由不同的組件使用秸抚,并且只有那些組件知道它速和。您也可以定義和使用Field項(xiàng)目中的任何其他 鍵,為您自己的需要剥汤。Field對(duì)象的主要目標(biāo) 是提供一種在一個(gè)地方定義所有字段元數(shù)據(jù)的方法颠放。通常,那些行為取決于每個(gè)字段的組件使用某些字段鍵來配置該行為吭敢。您必須參考他們的文檔碰凶,以查看每個(gè)組件使用哪些元數(shù)據(jù)鍵。
重要的是要注意鹿驼,F(xiàn)ield用于聲明項(xiàng)目的對(duì)象不會(huì)被分配為類屬性欲低。相反,可以通過Item.fields
屬性訪問它們畜晰。
使用項(xiàng)目
下面是使用上面聲明的Product項(xiàng)目對(duì)項(xiàng)目執(zhí)行的常見任務(wù)的一些示例 砾莱。你會(huì)注意到API非常類似于dict API。
創(chuàng)建項(xiàng)目
>>> 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
設(shè)置字段值
>>> product [ 'last_updated' ] = 'today'
>>> product [ 'last_updated' ]
today
>>> product [ 'lala' ] = 'test' #設(shè)置未知字段
Traceback(最近調(diào)用最后一次):
...
KeyError:'產(chǎn)品不支持字段:lala'
訪問所有填充值
要訪問所有填充值凄鼻,只需使用典型的dict API:
>>> product.keys()
['price', 'name']
>>> product.items()
[('price', 1000), ('name', 'Desktop PC')]
其他常見任務(wù)
復(fù)制項(xiàng)目:
>>> product2 = Product(product)
>>> print product2
Product(name='Desktop PC', price=1000)
>>> product3 = product2.copy()
>>> print product3
Product(name='Desktop PC', price=1000)
從項(xiàng)目創(chuàng)建詞典:
>>> dict(product) # create a dict from all populated values
{'price': 1000, 'name': 'Desktop PC'}
從短片創(chuàng)建項(xiàng)目:
>>> 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'
擴(kuò)展項(xiàng)目
您可以通過聲明原始項(xiàng)的子類來擴(kuò)展項(xiàng)(以添加更多字段或更改某些字段的某些元數(shù)據(jù))腊瑟。
例如:
class DiscountedProduct(Product):
discount_percent = scrapy.Field(serializer=str)
discount_expiration_date = scrapy.Field()
您還可以通過使用先前的字段元數(shù)據(jù)并附加更多值或更改現(xiàn)有值來擴(kuò)展字段元數(shù)據(jù)聚假,如下所示:
class SpecificProduct(Product):
name = scrapy.Field(Product.fields['name'], serializer=my_serializer)
添加(或替換)字段的serializer元數(shù)據(jù)鍵name,保留所有以前存在的元數(shù)據(jù)值闰非。
項(xiàng)目對(duì)象
class scrapy.item.Item([arg])
返回一個(gè)可以從給定參數(shù)初始化的新項(xiàng)目膘格。
項(xiàng)目復(fù)制標(biāo)準(zhǔn)dict API,包括其構(gòu)造函數(shù)财松。Items提供的唯一附加屬性是:
fields
包含字典中的所有聲明的字段為這個(gè)項(xiàng)目瘪贱,不僅是那些填充。鍵是字段名稱辆毡,值是Field
在項(xiàng)目聲明中使用的 對(duì)象菜秦。
字段對(duì)象
class scrapy.item.Field([arg])
Field
類只是一個(gè)別名內(nèi)置字典類和不提供任何額外的功能或?qū)傩浴Q句話說胚迫, Field
對(duì)象是普通的Python
代碼喷户。單獨(dú)的類用于支持 基于類屬性的項(xiàng)聲明語法唾那。