scrapy 使用crawlspider rule不起作用的解决方案

一直用的是通用spider,今天刚好想用下CrawlSpider来抓下数据。结果Debug了半天,一直没法进入详情页的解析逻辑。。

爬虫代码是这样的

# -*- coding: utf-8 -*-
import scrapy
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor


class BeihaireSpider(CrawlSpider):
    name = 'example'
    allowed_domains = ['example.gov.cn']
    start_urls = [
        'http://www.example.gov.cn/2j.asp?numberbj=13&id=106',
    ]
    rules = (
        Rule(LinkExtractor(allow=('unid',)), callback='parse'),
    )

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

Google、Baidu了好久,没找到原因,不知道是关键字搜索不够精准还是咋的。

然后就去翻Scrapy的文档,结果发现是parse函数的问题。

官方文档中关于crawlspider的一句话:

When writing crawl spider rules, avoid using parse as callback, since the CrawlSpider uses the parse method itself to implement its logic. So if you override the parse method, the crawl spider will no longer work.

终于找到原因了,原来crawlspider的实现用了parse函数来解析页面。而我,把它给覆盖了,所以一进到parse函数,爬虫就抓取结束了。。啥也没干

解决方案

解析Response函数不要使用parse这个名字。

参考资料:

  1. <https://docs.scrapy.org/en/latest/topics/spiders.html?highlight=crawlspider#crawling-rules

转载于:https://www.cnblogs.com/seozed/p/10977051.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值