10 Scrapy框架持久化存储

一.基于终端指令的持久化存储

  保证parse方法中有可迭代类型对象(通常为列表or字典)的返回,该返回值可以通过终端指令的形式写入指定格式的文件中进行持久化操作.

1 执行输出指定格式进行存储:将爬取到的数据写入不同格式的文件中进行存储
2     scrapy crawl 爬虫名称 -o xxx.json
3     scrapy crawl 爬虫名称 -o xxx.xml
4     scrapy crawl 爬虫名称 -o xxx.csv

二.基于管道的持久化存储

  scrapy框架中已经为我们专门集成好了高效,便捷的持久化操作功能,我们直接使用就可以.要使用scrapy的持久化操作功能.

1     items.py:数据结构模板文件。定义数据属性。
2     pipelines.py:管道文件。接收数据(items),进行持久化操作。
3 
4 持久化流程:
5     1.爬虫文件爬取到数据后,需要将数据封装到items对象中。
6     2.使用yield关键字将items对象提交给pipelines管道进行持久化操作。
7     3.在管道文件中的process_item方法中接收爬虫文件提交过来的item对象,然后编写持久化存储的代码将item对象中存储的数据进行持久化存储
8     4.settings.py配置文件中开启管道

 三.应用

  将糗事百科首页中的段子和作者数据爬取下来,然后进行持久化存储

-爬虫文件:qiubaiPro.py

 1 import scrapy
 2 from  secondblood.items import SecondbloodItem
 3 
 4 
 5 class QiubaiproSpider(spider):
 6     name = 'qiubaiPro'
 7     allowed_domains = ['www.qiushibaike.com']
 8     start_urls = ['http://www.qiushibaike.com/']
 9 
10     def parse(self,response):
11         odiv = response.xpath('//div[@id="content-left"]/div')
12         for div in odiv:
13             #xpath函数返回的为列表,列表中存放的数据为Selector类型的数据.我们解析到的内容封装到Selector对象中,需要调用extract()函数将解析到的内容从selector中取出.
14             author = div.xpath('.//div[@class="author clearfix"]//h2/text()').extract_first()
15             author = author.strip('
')#过滤空行
16             content = div.xpath('.//div[@class="content"]/span/text()').extract_first()
17             content = content.strip('
')#过滤空行
18 
19             #将解析到的数据封装至items对象中
20             item = SecondbloodItem()
21             item['author'] = author
22             item['content'] = content
23 
24             yield item#提交item到管道文件(pipelines.py)

- items文件:items.py

1 import scrapy
2 
3 class SecondbloodItem(scrapy.Item):
4     author = scrapy.Field()  #存储作者
5     content = scrapy.Filed()  #存储段子内容

- 管道文件 :pipelines.py

 1 class SecondbloodPipeline(object):
 2     #构造方法
 3     def __init__(self):
 4         self.fp = None  #定义一个文件描述符属性
 5     #下面都是重写父类方法:
 6     #开始爬虫时,执行一次
 7     def open_spider(self,spider):
 8         print('爬虫开始')
 9         self.fp = open('./data.txt','w')
10         
11      #因为该方法会被多次调用,所以文件的开启和关闭操作写在了另外两个只会各自执行一次的方法中
12     def process_item(self,item,spider):
13         #将爬虫程序提交的item进行持久化存储
14         self.fp.write(item['author'] + ':' + item['content'] + '
')
15         return item
16 
17     #结束爬虫时,各执行一次
18     def close_spider(self,spider):
19         self.fp.close()
20         print('爬虫结束')

- 配置文件: settings.py

1 #开启管道
2 ITEM_PIPELINES = {
3     'secondblood.pipelines.SecondbloodPipeline': 300, #300表示为优先级,值越小优先级越高
4 }

四.通过管道将数据存储到mysql

修改配置文件如下:

- pipelines.py文件

 1 import pymysql
 2 class QiubaiproPipelineByMysql(object):
 3     
 4     conn = none  #mysql的连接对象声明
 5     cursor = None  #mysql游标对象声明
 6     def open_spider(self,spider):
 7         print('爬虫开始')
 8         #连接数据库
 9         self.conn = pymysql.Connect(host='127.0.0.1',port=3306,user='root',password='123456',db='qiubai')
10         #编写向数据库中存储数据的相关代码
11      def process_item(self,item,spider):
12         #1.连接数据库
13         #2.执行sql语句
14         sql = 'insert into qiubai value("%s","%s")' % (item['author'],item['content'])
15         self.cursor = self.conn.cursor()
16         #执行事务
17         try:
18             self.cursor.execute(sql)
19             self.conn.commit()
20         except Exception as e:
21             print(e)
22             self.conn.rollback()
23 
24         return item
25 
26     def close_spider(self,spider):
27         print('爬虫结束')
28         self.cursor.close()
29         self.conn.close()

- settings.py

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

五.基于redis的管道存储

- pipelines.py

 1 import redis
 2 
 3 class QiubaiproPipelineByRedis(object):
 4     conn = None
 5     def open_spider(self,spider):
 6         print('开始爬虫')
 7         #创建链接对象
 8         self.conn = redis.Redis(host='127.0.0.1',port=6379)
 9     
10     def process_item(self, item, spider):
11         dict = {
12             'author':item['author'],
13             'content':item['content']
14         }
15         #写入redis中
16         self.conn.lpush('data', dict)
17         return item

- settings.py

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

如何将爬取到的数据一份存储到磁盘上,另一份存储到数据库中?

-pipelines.py

 1 #该类为管道类,该类中的process_item方法是用来实现持久化存储操作的。
 2 class DoublekillPipeline(object):
 3 
 4     def process_item(self, item, spider):
 5         #持久化操作代码 (方式1:写入磁盘文件)
 6         return item
 7 
 8 #如果想实现另一种形式的持久化操作,则可以再定制一个管道类:
 9 class DoublekillPipeline_db(object):
10 
11     def process_item(self, item, spider):
12         #持久化操作代码 (方式1:写入数据库)
13         return item

- settings.py开启管道操作代码为:

1 #下列结构为字典,字典中的键值表示的是即将被启用执行的管道文件和其执行的优先级。
2 ITEM_PIPELINES = {
3    'doublekill.pipelines.DoublekillPipeline': 300,
4     'doublekill.pipelines.DoublekillPipeline_db': 200,
5 }
6 
7 #上述代码中,字典中的两组键值分别表示会执行管道文件中对应的两个管道类中的process_item方法,实现两种不同形式的持久化操作。

https://www.cnblogs.com/bobo-zhang/p/10068997.html

原文地址:https://www.cnblogs.com/a2534786642/p/10981638.html