Scrapy 爬蟲框架 物品(Items)詳解

2021-10-18 13:44:46 字數 3756 閱讀 3512

開發環境為 python3.6,scrapy 版本 2.4.x ,爬蟲專案全部內容索引目錄

看懂python爬蟲框架,所見即所得一切皆有可能

本章帶你學習基於python3scrapy 爬蟲框架中資料爬取過程中從非結構化源(通常是網頁)中提取結構化資料。

**內容基於「scrapy 爬蟲框架」原始碼版本 2.4.0 ,更新內容會進行標記說明對應版本。

由於官網該部分內容介紹繁瑣僅了解即可,開發過程中需要根據自己的實際情況進行優化調整。

items提供了乙個可以讀取、寫入、修改的資料的字典供使用。

# dictionaries**:資料型別是字典。

# item objects**:擁有與字典相同的操作。

from scrapy.item import item, field

class

customitem

(item)

: one_field = field(

) another_field = field(

)

# dataclass objects:支援序列化定義專案資料中的資料型別

from dataclasses import dataclass

@dataclass

class

customitem

: one_field:

str another_field:

int

# attrs objects:支援序列化轉換數屬性

import attr

@attr.s

class

customitem

: one_field = attr.ib(

str)

another_field = attr.ib(convert=

float

)

# 項子類使用簡單的類定義語法和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)

# 建立 items

>>

> product = product(name=

'desktop pc'

, price=

1000

)>>

>

print

(product)

product(name=

'desktop pc'

, price=

1000

)

#  獲取items的值

>>

> product[

'name'

]desktop pc

>>

> product.get(

'name'

)desktop pc

>>

> product[

'price'

]1000

# 一般錯誤提示,同字典報錯

>>

> product[

'lala'

]# 獲取未定義的字段值

traceback (most recent call last):.

..keyerror:

'lala'

# 設定items的值

>>

> product[

'last_updated']=

'today'

>>

> product[

'last_updated'

]today

# 字典操作items

>>

> product.keys()[

'price'

,'name'

]>>

> product.items()[

('price'

,1000),

('name'

,'desktop pc'

)]

# 複製items

product2 = product.copy(

)# 或者

product2 = product(product)

# 字典建立items

>>

> product(

)product(price=

1500

, name=

'laptop pc'

)

# 資料型別擴充套件

# 直接定義資料型別

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)

from myproject.items import product

defparse

(self, response)

: item = product(

) item[

"name"

]= response.xpath(

'//div[@class="***"]/text()'

).extract(

) item[

"price"

]= response.xpath(

'//div[@class="***"]/text()'

).extract(

) item[

"stock"

]= response.xpath(

'//div[@class="***"]/text()'

).extract(

) item[

"tags"

]= response.xpath(

'//div[@class="***"]/text()'

).extract(

) item[

"last_updated"

]= response.xpath(

'//div[@class="***"]/text()'

).extract(

)yield item

scrapy爬蟲框架

作者經過幾周的python爬蟲實踐之後,深入學習了一下scrapy這個爬蟲框架,現將一些基本知識和 總結整理一下,以備後查。2.scrapy的命令列使用 這部分網上很多部落格都有總結,不需要背,理解會用主要的命令 startproject crawl fetch list genspider.即可,...

scrapy 爬蟲框架

1.安裝 公升級pip版本 pip install upgrade pip 通過pip安裝scrapy框架 pip install scrapy 安裝成功 只執行scrapy 進行測試是否安裝成功 2.scrapy startproject 爬蟲專案名稱 執行此命令,可以生成乙個爬蟲專案 會預先生成...

Scrapy爬蟲框架

scrapy中的各大元件及其功能 1.scrapy引擎 engine 引擎負責控制資料流在系統的所有元件中流動,並在相應動作發生時觸發事件。2.排程器 scheduler 排程器從引擎接收request並將它們入隊,以便之後引擎請求request時提供給引擎。4.spider。spider是scra...