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

2.基于管道的持久化存储
scrapy框架中已经为我们专门集成好了高效、便捷的持久化操作功能,我们直接使用即可。要想使用scrapy的持久化操作功能,我们首先来认识如下两个文件:

小试牛刀:将糗事百科首页中的段子和作者数据爬取下来,然后进行持久化存储
- 爬虫文件:qiubaiDemo.py

- items文件:items.py
.

- 管道文件:pipelines.py

- 配置文件:settings.py

2.1 基于mysql的管道存储
小试牛刀案例中,在管道文件里将item对象中的数据值存储到了磁盘中,如果将item数据写入mysql数据库的话,只需要将上述案例中的管道文件修改成如下形式:
- pipelines.py文件

- settings.py

2.2 基于redis的管道存储
小试牛刀案例中,在管道文件里将item对象中的数据值存储到了磁盘中,如果将item数据写入redis数据库的话,只需要将上述案例中的管道文件修改成如下形式:

- pipelines.py文件

- 面试题:如果最终需要将爬取到的数据值一份存储到磁盘文件,一份存储到数据库中,则应该如何操作scrapy?
- 答:管道文件中的代码为

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

网友评论