解决scrapy中创建请求,部分url丢失的情况

解决scrapy中创建请求,部分url丢失的情况

先感受一下发生的问题

    def parse(self, response):
        # print(response.text)
        book_url = response.xpath('//*[@class="r"]/ul/li/span[1]/a/@href').extract()
        print(len(book_url)) #30
        for url in book_url:
            yield scrapy.Request(url, callback=self.parse_book, dont_filter=True)

    def parse_book(self, response):
        print(response.url) 

遍历url_list中的url,分别创建请求进行二次访问。看一下令人头疼的结果。
在这里插入图片描述
30个url请求成功9个,丢失率超过2/3。

解决方法:

  • 1、 添加参数 dont_filter=True

      `yield scrapy.Request(url, callback=self.parse_book, dont_filter=True)`
    
  • 2、修改配置文件中的参数

    # Configure a delay for requests for the same website (default: 0)
    # See https://docs.scrapy.org/en/latest/topics/settings.html#download-delay
    # See also autothrottle settings and docs
    DOWNLOAD_DELAY = 3
    
    # The download delay setting will honor only one of:
    CONCURRENT_REQUESTS_PER_DOMAIN = 16
    CONCURRENT_REQUESTS_PER_IP = 16
    
  • 3、降低delay

    # Configure a delay for requests for the same website (default: 0)
    # See https://docs.scrapy.org/en/latest/topics/settings.html#download-delay
    # See also autothrottle settings and docs
    # DOWNLOAD_DELAY = 3
    DOWNLOAD_DELAY = 0.5
    # The download delay setting will honor only one of:
    CONCURRENT_REQUESTS_PER_DOMAIN = 16
    CONCURRENT_REQUESTS_PER_IP = 16
    
    

验证

在这里插入图片描述

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值