網站首頁 編程語言 正文
Item Pipeline簡介
Item管道的主要責任是負責處理有蜘蛛從網頁中抽取的Item,他的主要任務是清晰、驗證和存儲數據。
當頁面被蜘蛛解析后,將被發送到Item管道,并經過幾個特定的次序處理數據。
每個Item管道的組件都是有一個簡單的方法組成的Python類。
他們獲取了Item并執行他們的方法,同時他們還需要確定的是是否需要在Item管道中繼續執行下一步或是直接丟棄掉不處理。
調用時間: 當Item在Spider中被收集之后,它將會被傳遞到Item Pipeline,一些組件會按照一定的順序執行對Item的處理。
功能:
- 清理HTML數據
- 驗證爬取的數據(檢查item包含某些字段)
- 查重(并丟棄)
- 將爬取結果保存到數據庫中
一、一個自己的Pipeline類
必須實現以下方法:
process_item(self, item**,** spider**)**
每個item pipeline組件都需要調用該方法,這個方法必須返回一個具有數據的dict,或是 Item(或任何繼承類)對象, 或是拋出 DropItem 異常,被丟棄的item將不會被之后的pipeline組件所處理。
參數:
- item (Item 對象或者一個dict) – 被爬取的item
- spider (Spider 對象) – 爬取該item的spider
open_spider(self, spider)
當spider被開啟時,這個方法被調用。參數:spider (Spider對象) – 被開啟的spider
from_crawler(cls,crawler)
如果存在,則調用該類方法以從中創建管道實例Crawler。它必須返回管道的新實例。搜尋器對象提供對所有Scrapy核心組件(如設置和信號)的訪問;這是管道訪問它們并將其功能掛鉤到Scrapy中的一種方法。
close_spider(self, spider)
當spider被關閉時,這個方法被調用參數:spider (Spider對象) – 被關閉的spider
二、啟用一個Item Pipeline組件
為了啟用一個Item Pipeline組件,你必須將它的類添加到 ITEM_PIPELINES 配置,就像下面這個例子:
ITEM_PIPELINES = {
'myproject.pipelines.PricePipeline': 300,
'myproject.pipelines.JsonWriterPipeline': 800,
}
分配給每個類的整型值,確定了他們運行的順序,item按數字從低到高的順序,通過pipeline,通常將這些數字定義在0-1000范圍內。
將item寫入JSON文件
以下pipeline將所有爬取到的item,存儲到一個獨立地items.json 文件,每行包含一個序列化為'JSON'格式的'item':
import json
class JsonWriterPipeline(object):
def __init__(self):
self.file = open('items.json', 'wb')
def process_item(self, item, spider):
line = json.dumps(dict(item),ensure_ascii=False) + "\n"
self.file.write(line)
return item
在這里優化:
以下pipeline將所有爬取到的item,存儲到一個獨立地items.json 文件,每行包含一個序列化為'JSON'格式的'item':
import json
import codecs
class JsonWriterPipeline(object):
def __init__(self):
self.file = codecs.open('items.json', 'w', encoding='utf-8')
def process_item(self, item, spider):
line = json.dumps(dict(item), ensure_ascii=False) + "\n"
self.file.write(line)
return item
def spider_closed(self, spider):
self.file.close()
針對spider里面的utf-8編碼格式去掉.encode('utf-8')
item = RecruitItem()
item['name']=name.encode('utf-8')
item['detailLink']=detailLink.encode('utf-8')
item['catalog']=catalog.encode('utf-8')
item['recruitNumber']=recruitNumber.encode('utf-8')
item['workLocation']=workLocation.encode('utf-8')
item['publishTime']=publishTime.encode('utf-8')
將item寫入MongoDB
from_crawler(cls, crawler)
如果使用,這類方法被調用創建爬蟲管道實例。必須返回管道的一個新實例。crawler提供存取所有Scrapy核心組件配置和信號管理器;對于pipelines這是一種訪問配置和信號管理器 的方式。
在這個例子中,我們將使用pymongo將Item寫到MongoDB。MongoDB的地址和數據庫名稱在Scrapy setttings.py配置文件中;
這個例子主要是說明如何使用from_crawler()方法
import pymongo
class MongoPipeline(object):
collection_name = 'scrapy_items'
def __init__(self, mongo_uri, mongo_db):
self.mongo_uri = mongo_uri
self.mongo_db = mongo_db
@classmethod
def from_crawler(cls, crawler):
return cls(
mongo_uri=crawler.settings.get('MONGO_URI'),
mongo_db=crawler.settings.get('MONGO_DATABASE', 'items')
)
def open_spider(self, spider):
self.client = pymongo.MongoClient(self.mongo_uri)
self.db = self.client[self.mongo_db]
def close_spider(self, spider):
self.client.close()
def process_item(self, item, spider):
self.db[self.collection_name].insert(dict(item))
return item
原文鏈接:https://blog.csdn.net/zhongjianboy/article/details/123431523
相關推薦
- 2022-08-14 kvm虛擬機配置NAT端口轉發的實現方法_Kvm
- 2022-03-19 K8S中五種控制器的介紹以及使用_云其它
- 2022-07-16 解決IDEA:Class ‘XXX‘ not found in module ‘XXX‘
- 2023-01-20 C++利用模板實現消息訂閱和分發功能_C 語言
- 2023-04-07 C#?async/await任務超時處理的實現_C#教程
- 2023-07-10 Gateway服務網關
- 2022-12-01 Golang打印復雜結構體兩種方法詳解_Golang
- 2022-04-02 Docker鏡像發布到Docker?Hub的實現方法_docker
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支