(六--二)scrapy框架之持久化操作
scrapy框架之持久化操作
- 基于终端指令的持久化存储
- 基于管道的持久化存储
1 基于终端指令的持久化存储
- 保证爬虫文件的parse方法中有可迭代类型对象(通常为列表or字典)的返回,该返回值可以通过终端指令的形式写入指定格式的文件中进行持久化操作。
执行输出指定格式进行存储:将爬取到的数据写入不同格式的文件中进行存储
scrapy crawl 爬虫名称 -o xxx.json
scrapy crawl 爬虫名称 -o xxx.xml
scrapy crawl 爬虫名称 -o xxx.csv
以爬取糗事百科(https://www.qiushibaike.com/text/)为例
import scrapy class QiubaiSpider(scrapy.Spider):
name = 'qiubai' # 表示该爬虫文件的名称
allowed_domains = ['www.qiushibaike.com/text/']
start_urls = ['https://www.qiushibaike.com/text/']
# 解析函数
def parse(self, response): # response就是对起始url发起请求后,对应的响应对象
author_list = response.xpath('//div[@id="content-left"]/div') all_data = []
for div in author_list:
# extract_first()可以将xpath返回列表中第一个列表元素进行extract解析操作
author = div.xpath('./div/a[2]/h2/text()').extract_first()
# extract()可以将Selector对象中存储的数据进行解析操作
author = div.xpath('./div/a[2]/h2/text()').extract()
content = div.xpath('./a/div/span/text()').extract_first() dict={
'author':author,
'content':content
}
all_data.append(dict)
return all_data # 可迭代的对象
在终端写入
执行输出指定格式进行存储:将爬取到的数据写入不同格式的文件中进行存储
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 爬虫文件qiubai.py
# -*- coding: utf- -*-
import scrapy
from ..items import FirstProjectItem
'''基于管道存储''' '''
爬虫文件中解析数据
【items.py】将解析到的数据值全部分装在item对象中
pipelines.py
settings.py配置文件 '''
class QiubaiSpider(scrapy.Spider):
name = 'qiubai'
allowed_domains = ['www.qiushibaike.com/text/']
start_urls = ['https://www.qiushibaike.com/text/'] def parse(self, response): author_list = response.xpath('//div[@id="content-left"]/div') for div in author_list: author = div.xpath('./div/a[2]/h2/text()').extract_first()
# author = div.xpath('./div/a[2]/h2/text()')[].extract()
content = div.xpath('./a/div/span/text()').extract_first()
----------------------------------------------------
items = FirstProjectItem() items["author"] = author 重点
items["content"] = content
# 提交给管道
yield items
----------------------------------------------------
2 items.py
import scrapy # items会实例化一个items对象; 用来存储解析到的数据值 class FirstProjectItem(scrapy.Item):
# define the fields for your item here like:
-----------------------------------------
author = scrapy.Field()
content = scrapy.Field() 重点 你在第一步中有几个要持久化的这就写上对应的
-----------------------------------------
3 pipelines.py
# 爬虫文件每向管道提交一次item则process_item方法就会被执行一次
class FirstProjectPipeline(object):
# item就是爬虫文件提交过来的
def process_item(self, item, spider):
return item
4 settings.py
# 第67行
ITEM_PIPELINES = {
'first_project.pipelines.FirstProjectPipeline': ,
}
依据上面四步我们就学会了基本的“基于管道的持久化”的步骤,但是我们要在piplines.py做一些操作
只是修改第3步pipelines.py
# -*- coding: utf- -*- # 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 FirstProjectPipeline(object):
# 每次都会打开多次文件,我们重写 open_spider方法来开文件一次
fp = None
def open_spider(self, spider):
print('开始爬虫')
self.fp = open('qiubai1.txt', 'w', encoding='utf-8') def process_item(self, item, spider): self.fp.write(item['author']+':'+item["content"]+"\n") # 生成qiubai1.txt文件
return item def close_spider(self,spider):
print('结束爬虫')
self.fp.close()
3 写入数据库
import pymysql
class MysqlPipline(object):
cursor = None
conn = None
def open_spider(self, spider):
print('mysql开始')
self.conn = pymysql.connect(host='127.0.0.1', user='root', password='123456', port=3306, db='s18',charset='utf8')
def process_item(self, item, spider):
sql = "insert into t_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:
self.conn.rollback()
return item def close_spider(self, spider):
print('mysql结束')
self.cursor.close()
self.conn.close()
settings.py
ITEM_PIPELINES = {
'first_project.pipelines.FirstProjectPipeline': ,
'first_project.pipelines.MysqlPipline': , # settings 配置 值越小 越优先
}
4 写入redis数据库
import redis
class RedisPipline(object):
r = None
def open_spider(self, spider):
print('redis开始')
self.r = redis.Redis(host='127.0.0.1', port=6379)
def process_item(self, item, spider):
dict = {
'author':item['author'],
'content':item['content']
}
self.r.lpush('data', dict)
return item
def close_spider(self, spider):
print('redis结束')
settings.py设置
ITEM_PIPELINES = {
'first_project.pipelines.FirstProjectPipeline': 300,
'first_project.pipelines.RedisPipline': 500,
}
我们可以去redis里面查看
key * # 查看所有的key
lrange key 0 -1 # 从头到尾查看key
(六--二)scrapy框架之持久化操作的更多相关文章
- scrapy框架之持久化操作
1.基于终端指令的持久化存储 保证爬虫文件的parse方法中有可迭代类型对象(通常为列表or字典)的返回,该返回值可以通过终端指令的形式写入指定格式的文件中进行持久化操作. 执行输出指定格式进行存储: ...
- 爬虫开发8.scrapy框架之持久化操作
今日概要 基于终端指令的持久化存储 基于管道的持久化存储 今日详情 1.基于终端指令的持久化存储 保证爬虫文件的parse方法中有可迭代类型对象(通常为列表or字典)的返回,该返回值可以通过终端指令的 ...
- scrapy框架之分布式操作
分布式概念 分布式爬虫: 1.概念:多台机器上可以执行同一个爬虫程序,实现网站数据的分布爬取. 2.原生的scrapy是不可以实现分布式爬虫? a)调度器无法共享 b)管道无法共享 3.scrapy- ...
- 6 scrapy框架之分布式操作
分布式爬虫 一.redis简单回顾 1.启动redis: mac/linux: redis-server redis.conf windows: redis-server.exe redis-wi ...
- scrapy框架的持久化存储
一 . 基于终端指令的持久化存储 保证爬虫文件的parse方法中有可迭代类型对象(通常为列表or字典)的返回,该返回值可以通过终端指令的形式写入指定格式的文件中进行持久化操作. 执行输出指定格式进行存 ...
- 爬虫开发14.scrapy框架之分布式操作
分布式爬虫 一.redis简单回顾 1.启动redis: mac/linux: redis-server redis.conf windows: redis-server.exe redis-wi ...
- scrapy框架之CrawlSpider操作
提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法二:基 ...
- 爬虫开发11.scrapy框架之CrawlSpider操作
提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法二:基 ...
- Scrapy 框架,持久化文件相关
持久化相关 相关文件 items.py 数据结构模板文件.定义数据属性. pipelines.py 管道文件.接收数据(items),进行持久化操作. 持久化流程 1.爬虫文件爬取到数据后,需要将数据 ...
随机推荐
- java基础多线程
线程的创建 方式1:继承Java.lang.Thread类,并覆盖run() 方法 package com.demo.Thread; public class ThreadDemo01 extends ...
- hibernate中简单的增删改查
项目的整体结构如下 1.配置文件 hibernate.cfg.xml <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hi ...
- Day3-Q-修补木桶 HihoCoder1362
一只木桶能盛多少水,并不取决于桶壁上最高的那块木板,而恰恰取决于桶壁上最短的那块. 已知一个木桶的桶壁由N块木板组成,第i块木板的长度为Ai. 现在小Hi有一个快捷修补工具,每次可以使用修补工具将连续 ...
- Mac如何自定义本地化文件夹名
1. 关闭系统文件保护 在一切开始前,首先要先关闭掉系统的文件保护机制,否则无法修改系统文件,参见`如何关闭 Mac OS X EI Capitan 系统文件保护`这篇文章 2. 添加自定义本地化名称 ...
- rinetd 进行转发
目前云数据库 Redis 版需要通过 ECS 进行内网连接访问.如果您本地需要通过公网访问云数据库 Redis,可以在 ECS Linux 云服务器中安装 rinetd 进行转发实现. 在云服务器 E ...
- Luogu P3263 [JLOI2015]有意义的字符串
Link 设\(e=\frac{b+\sqrt d}2,i=\frac{b-\sqrt d}2\). 显然\(f_n=e^n+i^n\)是一个整数,且\(f_n=(e+i)f_{n-1}+eif_{n ...
- fuseki远程访问方法
./fuseki-server启动服务后,我们的服务只能是localhost访问,无法被其他人访问,那么 要怎么修改呢.很简单,把apche-jena-fuseki-3.10.0/run 下面的shi ...
- SChema 多个属性的设置学习
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http:/ ...
- idea中跑mapreduce报错, PATH设置错误
问题如题,报错: [root@node01 servers]# hadoop jar loginVisit.jar cn.itcast.loginVisit.step1.Step1Main19/07/ ...
- phpstudy后门漏洞复现php5.2
前段时间phpstudy被人发现某些版本存在后门,许多人就这样被当作肉鸡长达两年之久 后门隐藏在程序自带的php的php_xmlrpc.dll模块 影响的版本:phpstudy2016和2018 在H ...