Scrapy框架基础应用和持久化存储

一.Scrapy框架的基础应用python

1.Scrapy的概念mysql

  Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架,很是出名,很是强悍。所谓的框架就是一个已经被集成了各类功能(高性能异步下载,队列,分布式,解析,持久化等)的具备很强通用性的项目模板。redis

2.安装sql

  windows: 数据库

a. pip3 install wheel

b. 下载twisted http://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted

c. 进入下载目录,执行 pip3 install Twisted‑17.1.0‑cp35‑cp35m‑win_amd64.whl

d. pip3 install pywin32

e. pip3 install scrapy

  Linux:json

pip3 install scrapy

3.基础使用windows

  建立项目:     scrapy startproject  项目名称(project_name)数据结构

    项目结构并发

project_name/
   scrapy.cfg:
   project_name/
       __init__.py
       items.py
       pipelines.py
       settings.py
       spiders/
           __init__.py

scrapy.cfg   项目的主配置信息。(真正爬虫相关的配置信息在settings.py文件中)
items.py     设置数据存储模板,用于结构化数据,如:Django的Model
pipelines    数据持久化处理
settings.py  配置文件,如:递归的层数、并发数,延迟下载等
spiders      爬虫目录,如:建立文件,编写爬虫解析规则

  建立爬虫应用程序: app

cd project_name(进入项目目录)

scrapy genspider 应用名称 爬取网页的起始url (例如:scrapy genspider qiubai www.qiushibaike.com)

  编写爬虫文件:项目的spiders中生成一个应用名的py爬虫文件,文件源码以下:

import scrapy

class QiubaiSpider(scrapy.Spider):
    name = 'qiubai' #应用名称
    #容许爬取的域名(若是遇到非该域名的url则爬取不到数据)
    allowed_domains = ['https://www.qiushibaike.com/']
    #起始爬取的url
    start_urls = ['https://www.qiushibaike.com/']

     #访问起始URL并获取结果后的回调函数,该函数的response参数就是向起始的url发送请求后,获取的响应对象.该函数返回值必须为可迭代对象或者NUll 
     def parse(self, response):
        print(response.text) #获取字符串类型的响应内容
        print(response.body)#获取字节类型的相应内容

  设置修改setting.py配置文件:

修改内容及其结果以下:
USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36' #假装请求载体身份

ROBOTSTXT_OBEY = False  #能够忽略或者不遵照robots协议

  在终端执行程序:   scrapy crawl 应用名称

示例:

  将糗百首页中段子的内容和标题进行爬取

import scrapy

class QiubaiSpider(scrapy.Spider):
    name = 'qiubai'
    allowed_domains = ['https://www.qiushibaike.com/']
    start_urls = ['https://www.qiushibaike.com/']

    def parse(self, response):
        #xpath为response中的方法,能够将xpath表达式直接做用于该函数中
        odiv = response.xpath('//div[@id="content-left"]/div')
        content_list = [] #用于存储解析到的数据
        for div in odiv:
            #xpath函数返回的为列表,列表中存放的数据为Selector类型的数据。咱们解析到的内容被封装在了Selector对象中,须要调用extract()函数将解析的内容从Selecor中取出。
            author = div.xpath('.//div[@class="author clearfix"]/a/h2/text()')[0].extract()
            content=div.xpath('.//div[@class="content"]/span/text()')[0].extract()

            #将解析到的内容封装到字典中
            dic={
                '做者':author,
                '内容':content
            }
            #将数据存储到content_list这个列表中
            content_list.append(dic)

        return content_list

  在终端中执行爬虫程序:

 scrapy crawl 爬虫名称 :该种执行形式会显示执行的日志信息
    scrapy crawl 爬虫名称 --nolog:该种执行形式不会显示执行的日志信息

二.持久化存储

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

  保证爬虫文件的parse方法中有可迭代类型对象(一般为列表or字典)的返回,该返回值能够经过终端指令的形式写入指定格式的文件中进行持久化操做。

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


class FirstSpider(scrapy.Spider):
    #爬虫文件的名称
    name = 'first'
    #容许的域名
    #allowed_domains = ['www.xxx.com']
    #起始url列表
    start_urls = ['https://www.qiushibaike.com/text/']
    #实现了数据的基本解析操做
    # def parse(self, response):
    #     div_list = response.xpath('//div[@id="content-left"]/div')
    #     for div in div_list:
    #         #author = div.xpath('./div[1]/a[2]/h2/text()')[0].extract()
    #         #若是能够保证xpath返回的列表中只有一个列表元素则可使用extract_first(),不然必须使用extract()
    #         author = div.xpath('./div[1]/a[2]/h2/text()').extract_first()
    #         content = div.xpath('./a[1]/div/span//text()').extract()
    #         content = ''.join(content)
    #         print(author,content)

    #实现解析+持久化存储
    #1.基于终端指令的持久化存储
        # 只能够将parse方法的返回值持久化存储到本地的文本中

    def parse(self, response):
        div_list = response.xpath('//div[@id="content-left"]/div')
        all_data = []
        for div in div_list:
            #author = div.xpath('./div[1]/a[2]/h2/text()')[0].extract()
            #若是能够保证xpath返回的列表中只有一个列表元素则可使用extract_first(),不然必须使用extract()
            author = div.xpath('./div[1]/a[2]/h2/text()').extract_first()
            content = div.xpath('./a[1]/div/span//text()').extract()
            content = ''.join(content)

            dic = {
                'author':author,
                'content':content
            }

            all_data.append(dic)

        return all_data

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

  scrapy框架中已经为咱们专门集成好了高效、便捷的持久化操做功能,咱们直接使用

    items.py:数据结构模板文件。定义数据属性。
    pipelines.py:管道文件。接收数据(items),进行持久化操做。

持久化流程:
    1.爬虫文件爬取到数据后,须要将数据封装到items对象中。
    2.使用yield关键字将items对象提交给pipelines管道进行持久化操做。
    3.在管道文件中的process_item方法中接收爬虫文件提交过来的item对象,而后编写持久化存储的代码将item对象中存储的数据进行持久化存储
    4.settings.py配置文件中开启管道

示例:

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

qiubaiDemo.py

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.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     (管道文件)

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('爬虫结束')

  基于MySQL的管道存储

pipelines.py

#导入数据库的类
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,
}

  基于redis的管道存储

pipelines.py

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

setting.py

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

 三.scrapy框架之递归解析

1.递归爬取解析多页页面数据

  将糗事百科全部页码的做者和段子内容数据进行爬取切持久化存储

  使用request方法手动发起请求。

import scrapy
from qiushibaike.items import QiushibaikeItem
# scrapy.http import Request
class QiushiSpider(scrapy.Spider):
    name = 'qiushi'
    allowed_domains = ['www.qiushibaike.com']
    start_urls = ['https://www.qiushibaike.com/text/']

    #爬取多页
    pageNum = 1 #起始页码
    url = 'https://www.qiushibaike.com/text/page/%s/' #每页的url

    def parse(self, response):
        div_list=response.xpath('//*[@id="content-left"]/div')
        for div in div_list:
            #//*[@id="qiushi_tag_120996995"]/div[1]/a[2]/h2
            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')
            item=QiushibaikeItem()
            item['author']=author
            item['content']=content

            yield item #提交item到管道进行持久化

         #爬取全部页码数据
        if self.pageNum <= 13: #一共爬取13页(共13页)
            self.pageNum += 1
            url = format(self.url % self.pageNum)

            #递归爬取数据:callback参数的值为回调函数(将url请求后,获得的相应数据继续进行parse解析),递归调用parse函数
            yield scrapy.Request(url=url,callback=self.parse)
相关文章
相关标签/搜索