scrapy框架ItemPipeline的使用
Item Pipeline簡介
Item管道的主要責任是負責處理有蜘蛛從網(wǎng)頁中抽取的Item,他的主要任務是清晰、驗證和存儲數(shù)據(jù)。
當頁面被蜘蛛解析后,將被發(fā)送到Item管道,并經(jīng)過幾個特定的次序處理數(shù)據(jù)。
每個Item管道的組件都是有一個簡單的方法組成的Python類。
他們獲取了Item并執(zhí)行他們的方法,同時他們還需要確定的是是否需要在Item管道中繼續(xù)執(zhí)行下一步或是直接丟棄掉不處理。
調(diào)用時間: 當Item在Spider中被收集之后,它將會被傳遞到Item Pipeline,一些組件會按照一定的順序執(zhí)行對Item的處理。
功能:
- 清理HTML數(shù)據(jù)
- 驗證爬取的數(shù)據(jù)(檢查item包含某些字段)
- 查重(并丟棄)
- 將爬取結(jié)果保存到數(shù)據(jù)庫中
一、一個自己的Pipeline類
必須實現(xiàn)以下方法:
process_item(self, item**,** spider**)**
每個item pipeline組件都需要調(diào)用該方法,這個方法必須返回一個具有數(shù)據(jù)的dict,或是 Item(或任何繼承類)對象, 或是拋出 DropItem 異常,被丟棄的item將不會被之后的pipeline組件所處理。
參數(shù):
- item (Item 對象或者一個dict) – 被爬取的item
- spider (Spider 對象) – 爬取該item的spider
open_spider(self, spider)
當spider被開啟時,這個方法被調(diào)用。參數(shù):spider (Spider對象) – 被開啟的spider
from_crawler(cls,crawler)
如果存在,則調(diào)用該類方法以從中創(chuàng)建管道實例Crawler。它必須返回管道的新實例。搜尋器對象提供對所有Scrapy核心組件(如設置和信號)的訪問;這是管道訪問它們并將其功能掛鉤到Scrapy中的一種方法。
close_spider(self, spider)
當spider被關閉時,這個方法被調(diào)用參數(shù):spider (Spider對象) – 被關閉的spider
二、啟用一個Item Pipeline組件
為了啟用一個Item Pipeline組件,你必須將它的類添加到 ITEM_PIPELINES 配置,就像下面這個例子:
ITEM_PIPELINES = {
'myproject.pipelines.PricePipeline': 300,
'myproject.pipelines.JsonWriterPipeline': 800,
}分配給每個類的整型值,確定了他們運行的順序,item按數(shù)字從低到高的順序,通過pipeline,通常將這些數(shù)字定義在0-1000范圍內(nèi)。
將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在這里優(yōu)化:
以下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)
如果使用,這類方法被調(diào)用創(chuàng)建爬蟲管道實例。必須返回管道的一個新實例。crawler提供存取所有Scrapy核心組件配置和信號管理器;對于pipelines這是一種訪問配置和信號管理器 的方式。
在這個例子中,我們將使用pymongo將Item寫到MongoDB。MongoDB的地址和數(shù)據(jù)庫名稱在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到此這篇關于scrapy框架ItemPipeline的使用的文章就介紹到這了,更多相關scrapy ItemPipeline內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
pytorch中transform.Compose()用法詳解
PyTorch是一個開源的Python機器學習庫,基于Torch,用于自然語言處理等應用程序,這篇文章主要介紹了pytorch中transform.Compose()用法,需要的朋友可以參考下2023-10-10
python遍歷 truple list dictionary的幾種方法總結(jié)
下面小編就為大家?guī)硪黄猵ython遍歷 truple list dictionary的幾種方法總結(jié)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-09-09
python使用matplotlib定制繪圖的線型、標記類型
這篇文章主要給大家詳細介紹了python使用matplotlib定制繪圖的線型、標記類型,文中有詳細的代碼示例,具有一定的參考價值,需要的朋友可以參考下2023-07-07

