python爬虫 scrapy框架介绍和基础应用

一.什么是Scrapy?

  Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架,非常出名,非常强悍。所谓的框架就是一个已经被集成了各种功能(高性能异步下载,队列,分布式,解析,持久化等)的具有很强通用性的项目模板。
对于框架的学习,重点是要学习其框架的特性、各个功能的用法即可。

 

二。安装

Linux:

      pip3 install scrapy

 
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
scrapy安装引导

三.基础使用

1.创建项目:scrapy startproject 项目名称

项目结构:

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      爬虫目录,如:创建文件,编写爬虫解析规则

2.创建爬虫应用程序:

cd project_name(进入项目目录)
scrapy genspider 应用名称 爬取网页的起始url (例如:scrapy genspider qiubai www.qiushibaike.com)

3.编写爬虫文件:在步骤2执行完毕后,会在项目的spiders中生成一个应用名的py爬虫文件,文件源码如下

# -*- coding: utf-8 -*-
import scrapy

class FirstSpider(scrapy.Spider):
    name = 'first' #应用名称
    #允许爬取的域名(如果遇到非该域名的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)#获取字节类型的相应内容

4.设置修改settings.py配置文件相关配置:

修改内容及其结果如下:
19行: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' #伪装请求载体身份
22行:ROBOTSTXT_OBEY = False  #可以忽略或者不遵守robots协议

 5.执行爬虫程序:scrapy crawl  应用名称

xpath方法返回的是列表,列表元素是Selector对象,可以使用extract系列的方法将Selector对象中存储的数据提取出来

.extract()   将列表中的每一个元素分别解析

.[0]extract()   函数将解析列表中的第1个
.extract_first()  函数将解析列表中的第1个
# -*- coding: utf-8 -*-
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:该种执行形式不会显示执行的日志信息

四。scrapy框架持久化存储

介绍:

持久化存储分类两种:
       基于终端指定的持久化存储
       基于管道的持久化存储

1.基于终端指定的持久化存储:
    保证爬虫文件的parse方法中有可迭代类型对象(通常为列表or字典)的返回,该返回值可以通过终端指令的形式写入指定格式的文件中进行持久化操作。
    执行命令:执行输出指定格式进行存储:将爬取到的数据写入不同格式的文件中进行存储
         scrapy crawl 爬虫名称 -o xxx.(json,xml,csv)

2.基于管道的持久化存储
    scrapy框架中已经为我们专门集成好了高效、便捷的持久化操作功能,我们直接使用即可。
        items.py:数据结构模板文件。定义数据属性。
        pipelines.py:管道文件。接收数据(items),进行持久化操作。

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

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

scrapy startproject qiubai_test
csrapy genspider qiubai www.xxx.com
scrapy crawl qiubai --nolog
# -*- coding: utf-8 -*-
import scrapy
from qiubai_test.items  import QiubaiTestItem

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

    def parse(self, response):
        # 数据解析(xpath)
        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('
') #过滤空行
            content = div.xpath('.//div[@class="content"]/span/text()').extract_first()
            content = content.strip('
')  # 过滤空行

            # 将解析到的数据封装至items对象中
            item = QiubaiTestItem()  #实例化一个item类型的对象
            item['author'] = author
            item['content'] = content

            # 将item提交给管道
            yield item
爬虫文件代码
# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/items.html

import scrapy


class QiubaiTestItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    # pass
    author = scrapy.Field() #存储作者
    content = scrapy.Field() #存储段子内容
items文件:items.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 QiubaiTestPipeline(object):
    def __init__(self):
        self.fp = None  #定义一个文件描述符属性

    # 下列都是在重写父类的方法:
    # 只会被调用一次
    def open_spider(self, spider):
        print('爬虫开始')
        self.fp = open('./data.txt','w',encoding='utf-8')

    # 因为该方法会被执行调用多次,所以文件的开启和关闭操作写在了另外两个只会各自执行一次的方法中。
    # 该方法被调用后,就可以将item(参数)包含的数据值进行持久化存储
    def process_item(self, item, spider):
        # 将爬虫程序提交的item进行持久化存储
        self.fp.write(item['author'] + item['content'] + '
')
        # 返回给了下一个即将被执行的管道类
        return item

    # 结束爬虫时,执行一次
    def close_spider(self, spider):
        self.fp.close()
        print('爬虫结束')
管道文件:pipelines.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

ITEM_PIPELINES = {
   'qiubai_test.pipelines.QiubaiTestPipeline': 300, #300表示为优先级,值越小优先级越高
}
配置文件修改项:settings.py

2.基于本地磁盘存储与mysql的管道存储

上一个案例中,在管道文件里将item对象中的数据值存储到了磁盘中,如果将item数据写入mysql数据库的话,只需要将上述案例中的管道文件修改成如下形式:

# -*- 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 QiubaiTestPipeline(object):
    def __init__(self):
        self.fp = None  #定义一个文件描述符属性

    # 下列都是在重写父类的方法:
    # 只会被调用一次
    def open_spider(self, spider):
        print('爬虫开始')
        self.fp = open('./data.txt','w',encoding='utf-8')

    # 因为该方法会被执行调用多次,所以文件的开启和关闭操作写在了另外两个只会各自执行一次的方法中。
    # 该方法被调用后,就可以将item(参数)包含的数据值进行持久化存储
    def process_item(self, item, spider):
        # 将爬虫程序提交的item进行持久化存储
        self.fp.write(item['author'] + item['content'] + '
')
        # 返回给了下一个即将被执行的管道类
        return item

    # 结束爬虫时,执行一次
    def close_spider(self, spider):
        self.fp.close()
        print('爬虫结束')

# 使用MySQL持久化存储
import pymysql
class QiubaiTestPipelineByMysql(object):
    conn = None    # mysql的连接对象声明
    cursor = None  # mysql游标对象声明

    def open_spider(self, spider):
        print('mysql爬虫开始')
        # 链接数据库
        self.conn = pymysql.Connect(host='172.16.6.218', port=3306, user='maillog', password='123456', db='scrapytest')
    # 编写向数据库中存储数据的相关代码
    def process_item(self, item, spider):
        # 1.链接数据库
        # 2.执行sql语句
        self.cursor = self.conn.cursor()
        try:
            sql = "INSERT INTO qiubai VALUES('%s','%s')" %(item['author'], item['content'])
            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()
管道文件:pipelines.py
ITEM_PIPELINES = {
   'qiubai_test.pipelines.QiubaiTestPipeline': 300, #300表示为优先级,值越小优先级越高
   'qiubai_test.pipelines.QiubaiTestPipelineByMysql': 301,
}
配置文件修改项:settings.py

 3.基于redis的管道存储

如果将item数据写入到redis数据库的话,只需要将上述案例中的管道文件修改成如下形式:

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 = {
   'qiubai_test.pipelines.QiubaiproPipelineByRedis': 300,
}
配置文件修改项:settings.py

 五。scrapy框架之递归解析和post请求

实现递归爬取解析多页页面数据(get请求):

需求:将糗事百科所有页码的作者和段子内容数据进行爬取切持久化存储。
需求分析:每一个页面对应一个url,则scrapy工程需要对每一个页码对应的url依次发起请求,然后通过对应的解析方法进行作者和段子内容的解析。
实现方法:使用Request方法手动发起请求。
# -*- coding: utf-8 -*-
import scrapy
from qiubai_test.items  import QiubaiTestItem

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

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

    def parse(self, response):
        # 数据解析(xpath)
        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('
') #过滤空行
            content = div.xpath('.//div[@class="content"]/span/text()').extract_first()
            content = content.strip('
')  # 过滤空行

            # 将解析到的数据封装至items对象中
            item = QiubaiTestItem()  #实例化一个item类型的对象
            item['author'] = author
            item['content'] = content

            # 将item提交给管道
            yield item

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

            # 递归爬取数据:callback参数的值为回调函数(将url请求后,得到的相应数据继续进行parse解析),递归调用parse函数
            yield scrapy.Request(url=url, callback=self.parse)
管道文件:pipelines.py

 

五大核心组件工作流程描述:https://www.cnblogs.com/iamjianghao/p/10862947.html

post请求发送方法:爬虫文件中的爬虫类继承到了Spider父类中的start_requests(self)这个方法,该方法就可以对start_urls列表中的url发起请求。

  def start_requests(self):
        for u in self.start_urls:
           yield scrapy.Request(url=u,callback=self.parse)

【注意】该方法默认的实现,是对起始的url发起get请求,如果想发起post请求,则需要子类重写该方法。

  -方法: 重写start_requests方法,让其发起post请求:

def start_requests(self):
        #请求的url
        post_url = 'http://fanyi.baidu.com/sug'
        # post请求参数
        formdata = {
            'kw': 'wolf',
        }
        # 发送post请求
        yield scrapy.FormRequest(url=post_url, formdata=formdata, callback=self.parse)

六。scrapy 框架的日志等级和请求传参

 1.Scrapy的日志等级:

在使用scrapy crawl spiderFileName运行程序时,在终端里打印输出的就是scrapy的日志信息。

日志信息的种类:
    ERROR : 一般错误
    WARNING : 警告
    INFO : 一般的信息
    DEBUG : 调试信息

设置日志信息指定输出:
    在settings.py配置文件中,加入
         LOG_LEVEL = ‘指定日志信息种类’即可。
         LOG_FILE = 'log.txt'则表示将日志信息写入到指定文件中进行存储。    

2.请求传参:

示例一需求描述:
    在某些情况下,我们爬取的数据不在同一个页面中,例如,我们爬取一个电影网站,电影的名称,评分在一级页面,而要爬取的其他电影详情在其二级子页面中。这时我们就需要用到请求传参。
案例展示:
    爬取www.id97.com电影网,将一级页面中的电影名称,类型,评分一级二级页面中的上映时间,导演,片长进行爬取。
主要功能点:
  请求二级详情页面,解析二级页面中的相应内容,通过meta参数进行Request的数据传递:
  yield scrapy.Request(url=item[url链接],callback=二级解析函数,meta={'item':item})
# -*- coding: utf-8 -*-
import scrapy
from moviePro.items import MovieproItem

class MovieSpider(scrapy.Spider):
    name = 'movie'
    allowed_domains = ['www.id97.com']
    start_urls = ['http://www.id97.com/']

    def parse(self, response):
        div_list = response.xpath('//div[@class="col-xs-1-5 movie-item"]')

        for div in div_list:
            item = MovieproItem()
            item['name'] = div.xpath('.//h1/a/text()').extract_first()
            item['score'] = div.xpath('.//h1/em/text()').extract_first()
            #xpath(string(.))表示提取当前节点下所有子节点中的数据值(.)表示当前节点
            item['kind'] = div.xpath('.//div[@class="otherinfo"]').xpath('string(.)').extract_first()
            item['detail_url'] = div.xpath('./div/a/@href').extract_first()
            #请求二级详情页面,解析二级页面中的相应内容,通过meta参数进行Request的数据传递
            yield scrapy.Request(url=item['detail_url'],callback=self.parse_detail,meta={'item':item})

    def parse_detail(self,response):
        #通过response获取item
        item = response.meta['item']
        item['actor'] = response.xpath('//div[@class="row"]//table/tr[1]/a/text()').extract_first()
        item['time'] = response.xpath('//div[@class="row"]//table/tr[7]/td[2]/text()').extract_first()
        item['long'] = response.xpath('//div[@class="row"]//table/tr[8]/td[2]/text()').extract_first()
        #提交item到管道
        yield item
爬虫文件:
# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/items.html

import scrapy


class MovieproItem(scrapy.Item):
    # define the fields for your item here like:
    name = scrapy.Field()
    score = scrapy.Field()
    time = scrapy.Field()
    long = scrapy.Field()
    actor = scrapy.Field()
    kind = scrapy.Field()
    detail_url = scrapy.Field()
items文件:items.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 json
class MovieproPipeline(object):
    def __init__(self):
        self.fp = open('data.txt','w')
    def process_item(self, item, spider):
        dic = dict(item)
        print(dic)
        json.dump(dic,self.fp,ensure_ascii=False)
        return item
    def close_spider(self,spider):
        self.fp.close()
管道文件:pipelines.py
示例二需求 :爬取58同城二手房名称以及户型描述等信息,知识点是对二级页面数据解析传参
# -*- coding: utf-8 -*-
import scrapy
from housePro.items import HouseproItem

class HouseSpider(scrapy.Spider):
    name = 'house'
    # allowed_domains = ['www.xxx.com']
    start_urls = ['https://bj.58.com/changping/ershoufang/']
    def paesr_detail(self,response):
        #获取了请求传参传递过来的meta
        name = response.meta['name']
        item = HouseproItem()
        huxing = response.xpath('/html/body/div[4]/div[2]/div[2]/div[1]/p[1]/span[1]/text()').extract_first()
        item['name'] = name
        item['huxing'] = huxing
        
        yield item
        
    def parse(self, response):
        #房屋名称(首页)和户型(详情页)
        li_list = response.xpath('//ul[@class="house-list-wrap"]/li')
        for li in li_list:
            name = li.xpath('./div[2]/h2/a/text()').extract_first()
            detail_url = li.xpath('./div[2]/h2/a/@href').extract_first()
            print(detail_url)
            #对详情页url发起请求获取详情页数据解析出 户型
            yield scrapy.Request(url=detail_url,callback=self.paesr_detail,meta={'name':name})

           
爬虫文件
# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/items.html

import scrapy


class HouseproItem(scrapy.Item):
    # define the fields for your item here like:
    name = scrapy.Field()
    huxing = scrapy.Field()
items.py
BOT_NAME = 'housePro'

SPIDER_MODULES = ['housePro.spiders']
NEWSPIDER_MODULE = 'housePro.spiders'

USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36'

ROBOTSTXT_OBEY = False

ITEM_PIPELINES = {
   'housePro.pipelines.HouseproPipeline': 300,
}

LOG_LEVEL = 'ERROR'
配置文件:settings.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 HouseproPipeline(object):
    def process_item(self, item, spider):
        print(item)
        return item
pipelines.py

3.如何提高scrapy的爬虫效率

增加并发:
    默认scrapy开启的并发线程为32个,可以适当进行增加。在settings配置文件中修改CONCURRENT_REQUESTS = 100值为100,并发设置成了为100。
降低日志级别:
    在运行scrapy时,会有大量日志信息的输出,为了减少CPU的使用率。可以设置log输出信息为INFO或者ERROR即可。在配置文件中编写:LOG_LEVEL = ‘INFO’
禁止cookie:
    如果不是真的需要cookie,则在scrapy爬取数据时可以进制cookie从而减少CPU的使用率,提升爬取效率。在配置文件中编写:COOKIES_ENABLED = False
禁止重试:
    对失败的HTTP进行重新请求(重试)会减慢爬取速度,因此可以禁止重试。在配置文件中编写:RETRY_ENABLED = False
减少下载超时:
    如果对一个非常慢的链接进行爬取,减少下载超时可以能让卡住的链接快速被放弃,从而提升效率。在配置文件中进行编写:DOWNLOAD_TIMEOUT = 10 超时时间为10s

测试案例:爬取校花网校花图片 www.521609.com

# -*- coding: utf-8 -*-
import scrapy
from xiaohua.items import XiaohuaItem

class XiahuaSpider(scrapy.Spider):

    name = 'xiaohua'
    allowed_domains = ['www.521609.com']
    start_urls = ['http://www.521609.com/daxuemeinv/']

    pageNum = 1
    url = 'http://www.521609.com/daxuemeinv/list8%d.html'

    def parse(self, response):
        li_list = response.xpath('//div[@class="index_img list_center"]/ul/li')
        for li in li_list:
            school = li.xpath('./a/img/@alt').extract_first()
            img_url = li.xpath('./a/img/@src').extract_first()

            item = XiaohuaItem()
            item['school'] = school
            item['img_url'] = 'http://www.521609.com' + img_url

            yield item

        if self.pageNum < 10:
            self.pageNum += 1
            url = format(self.url % self.pageNum)
            #print(url)
            yield scrapy.Request(url=url,callback=self.parse)
爬虫文件
# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/items.html

import scrapy


class XiaohuaItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    school=scrapy.Field()
    img_url=scrapy.Field()
items文件:items.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 json
import os
import urllib.request
class XiaohuaPipeline(object):
    def __init__(self):
        self.fp = None

    def open_spider(self,spider):
        print('开始爬虫')
        self.fp = open('./xiaohua.txt','w')

    def download_img(self,item):
        url = item['img_url']
        fileName = item['school']+'.jpg'
        if not os.path.exists('./xiaohualib'):
            os.mkdir('./xiaohualib')
        filepath = os.path.join('./xiaohualib',fileName)
        urllib.request.urlretrieve(url,filepath)
        print(fileName+"下载成功")

    def process_item(self, item, spider):
        obj = dict(item)
        json_str = json.dumps(obj,ensure_ascii=False)
        self.fp.write(json_str+'
')

        #下载图片
        self.download_img(item)
        return item

    def close_spider(self,spider):
        print('结束爬虫')
        self.fp.close()
管道文件:pipelines.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'

# Obey robots.txt rules
ROBOTSTXT_OBEY = False

# Configure maximum concurrent requests performed by Scrapy (default: 16)
CONCURRENT_REQUESTS = 100
COOKIES_ENABLED = False
LOG_LEVEL = 'ERROR'
RETRY_ENABLED = False
DOWNLOAD_TIMEOUT = 3
# Configure a delay for requests for the same website (default: 0)
# See https://doc.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
# The download delay setting will honor only one of:
#CONCURRENT_REQUESTS_PER_DOMAIN = 16
#CONCURRENT_REQUESTS_PER_IP = 16
DOWNLOAD_DELAY = 3
配置文件
原文地址:https://www.cnblogs.com/iamjianghao/p/10846023.html