11.scrapy框架持久化存儲

今日概要html

  • 基於終端指令的持久化存儲
  • 基於管道的持久化存儲

今日詳情python

1.基於終端指令的持久化存儲mysql

  • 保證爬蟲文件的parse方法中有可迭代類型對象(一般爲列表or字典)的返回,該返回值能夠經過終端指令的形式寫入指定格式的文件中進行持久化操做。
執行輸出指定格式進行存儲:將爬取到的數據寫入不一樣格式的文件中進行存儲
    scrapy crawl 爬蟲名稱 -o xxx.json
    scrapy crawl 爬蟲名稱 -o xxx.xml
    scrapy crawl 爬蟲名稱 -o xxx.csv

2.基於管道的持久化存儲面試

scrapy框架中已經爲咱們專門集成好了高效、便捷的持久化操做功能,咱們直接使用便可。要想使用scrapy的持久化操做功能,咱們首先來認識以下兩個文件:redis

items.py:數據結構模板文件。定義數據屬性。
    pipelines.py:管道文件。接收數據(items),進行持久化操做。

持久化流程:
    1.爬蟲文件爬取到數據後,須要將數據封裝到items對象中。
    2.使用yield關鍵字將items對象提交給pipelines管道進行持久化操做。
    3.在管道文件中的process_item方法中接收爬蟲文件提交過來的item對象,而後編寫持久化存儲的代碼將item對象中存儲的數據進行持久化存儲
    4.settings.py配置文件中開啓管道

小試牛刀:將糗事百科首頁中的段子和做者數據爬取下來,而後進行持久化存儲sql

- 爬蟲文件:qiubaiDemo.py數據庫

# -*- coding: utf-8 -*- import scrapy from secondblood.items import SecondbloodItem class QiubaidemoSpider(scrapy.Spider): name = 'qiubaiDemo' allowed_domains = ['www.qiushibaike.com'] start_urls = ['http://www.qiushibaike.com/'] def parse(self, response): odiv = response.xpath('//div[@id="content-left"]/div') for div in odiv: # xpath函數返回的爲列表,列表中存放的數據爲Selector類型的數據。咱們解析到的內容被封裝在了Selector對象中,須要調用extract()函數將解析的內容從Selecor中取出。 author = div.xpath('.//div[@class="author clearfix"]//h2/text()').extract_first() author = author.strip('\n')#過濾空行 content = div.xpath('.//div[@class="content"]/span/text()').extract_first() content = content.strip('\n')#過濾空行 #將解析到的數據封裝至items對象中 item = SecondbloodItem() item['author'] = author item['content'] = content yield item#提交item到管道文件(pipelines.py)

- items文件:items.pyjson

import scrapy class SecondbloodItem(scrapy.Item): # define the fields for your item here like: # name = scrapy.Field() author = scrapy.Field() #存儲做者 content = scrapy.Field() #存儲段子內容

- 管道文件:pipelines.py數據結構

# -*- coding: utf-8 -*- # Define your item pipelines here # # Don't forget to add your pipeline to the ITEM_PIPELINES setting # See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html class SecondbloodPipeline(object): #構造方法 def __init__(self): self.fp = None #定義一個文件描述符屬性   #下列都是在重寫父類的方法: #開始爬蟲時,執行一次 def open_spider(self,spider): print('爬蟲開始') self.fp = open('./data.txt', 'w')    #由於該方法會被執行調用屢次,因此文件的開啓和關閉操做寫在了另外兩個只會各自執行一次的方法中。 def process_item(self, item, spider): #將爬蟲程序提交的item進行持久化存儲 self.fp.write(item['author'] + ':' + item['content'] + '\n') return item #結束爬蟲時,執行一次 def close_spider(self,spider): self.fp.close() print('爬蟲結束')

- 配置文件:settings.pyapp

#開啓管道 ITEM_PIPELINES = { 'secondblood.pipelines.SecondbloodPipeline': 300, #300表示爲優先級,值越小優先級越高 }

2.1 基於mysql的管道存儲

小試牛刀案例中,在管道文件裏將item對象中的數據值存儲到了磁盤中,若是將item數據寫入mysql數據庫的話,只須要將上述案例中的管道文件修改爲以下形式:

- pipelines.py文件

# -*- coding: utf-8 -*- # Define your item pipelines here # # Don't forget to add your pipeline to the ITEM_PIPELINES setting # See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html #導入數據庫的類 import pymysql class QiubaiproPipelineByMysql(object): conn = None #mysql的鏈接對象聲明 cursor = None#mysql遊標對象聲明 def open_spider(self,spider): print('開始爬蟲') #連接數據庫 self.conn = pymysql.Connect(host='127.0.0.1',port=3306,user='root',password='123456',db='qiubai') #編寫向數據庫中存儲數據的相關代碼 def process_item(self, item, spider): #1.連接數據庫 #2.執行sql語句 sql = 'insert into qiubai values("%s","%s")'%(item['author'],item['content']) self.cursor = self.conn.cursor() #執行事務 try: self.cursor.execute(sql) self.conn.commit() except Exception as e: print(e) self.conn.rollback() return item def close_spider(self,spider): print('爬蟲結束') self.cursor.close() self.conn.close() 

- settings.py

ITEM_PIPELINES = {
    'qiubaiPro.pipelines.QiubaiproPipelineByMysql': 300, }

2.2 基於redis的管道存儲

小試牛刀案例中,在管道文件裏將item對象中的數據值存儲到了磁盤中,若是將item數據寫入redis數據庫的話,只須要將上述案例中的管道文件修改爲以下形式:

# -*- coding: utf-8 -*- # Define your item pipelines here # # Don't forget to add your pipeline to the ITEM_PIPELINES setting # See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html import redis class QiubaiproPipelineByRedis(object): conn = None def open_spider(self,spider): print('開始爬蟲') #建立連接對象 self.conn = redis.Redis(host='127.0.0.1',port=6379) def process_item(self, item, spider): dict = { 'author':item['author'], 'content':item['content'] } #寫入redis中 self.conn.lpush('data', dict) return item 

- pipelines.py文件 

ITEM_PIPELINES = {
    'qiubaiPro.pipelines.QiubaiproPipelineByRedis': 300, }
 

- 面試題:若是最終須要將爬取到的數據值一份存儲到磁盤文件,一份存儲到數據庫中,則應該如何操做scrapy?  

- 答:管道文件中的代碼爲

#該類爲管道類,該類中的process_item方法是用來實現持久化存儲操做的。 class DoublekillPipeline(object): def process_item(self, item, spider): #持久化操做代碼 (方式1:寫入磁盤文件) return item #若是想實現另外一種形式的持久化操做,則能夠再定製一個管道類: class DoublekillPipeline_db(object): def process_item(self, item, spider): #持久化操做代碼 (方式1:寫入數據庫) return item

 在settings.py開啓管道操做代碼爲:

#下列結構爲字典,字典中的鍵值表示的是即將被啓用執行的管道文件和其執行的優先級。 ITEM_PIPELINES = { 'doublekill.pipelines.DoublekillPipeline': 300, 'doublekill.pipelines.DoublekillPipeline_db': 200, } #上述代碼中,字典中的兩組鍵值分別表示會執行管道文件中對應的兩個管道類中的process_item方法,實現兩種不一樣形式的持久化操做。
相關文章
相關標籤/搜索