1. 程式人生 > >scrapy框架持久化存儲

scrapy框架持久化存儲

終端 spa sele you base64 strip 取出 extra esp

1.概要

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

2.詳情

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

  • 保證爬蟲文件的parse方法中有可叠代類型對象(通常為列表or字典)的返回,該返回值可以通過終端指令的形式寫入指定格式的文件中進行持久化操作。
技術分享圖片
執行輸出指定格式進行存儲:將爬取到的數據寫入不同格式的文件中進行存儲
    scrapy crawl 爬蟲名稱 -o xxx.json
    scrapy crawl 爬蟲名稱 -o xxx.xml
    scrapy crawl 爬蟲名稱 -o xxx.csv

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

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

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

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

(1).小試牛刀:將糗事百科首頁中的段子和作者數據爬取下來,然後進行持久化存儲

- 爬蟲文件: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.py

技術分享圖片
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.py

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

(2).基於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,
}

(3). 基於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方法,實現兩種不同形式的持久化操作。

scrapy框架持久化存儲