DEBUG: Filtered offsite request to

在做爬虫项目时,出现了一个问题,解析一个网站二次爬取时没有获取到数据,就写了一个测试程序试了下,测试程序如下

import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule




class ZhenaiSpider(CrawlSpider):
    name = 'zhenai'
    allowed_domains = ['www.zhenai.com']
    start_urls = ['http://www.zhenai.com/zhenghun/beijing/1']


    rules = (
        Rule(LinkExtractor(allow=r'http://www.zhenai.com/zhenghun/beijing/\d+'), callback='parse_item', follow=False),
    )


    def parse_item(self, response):
        a_list = response.xpath('//div[@class="content"]/table//tr/th/a')
        for a in a_list:
            item = {}
            title = a.xpath('./text()').extract_first()
            item['title'] = title
            detail_url = a.xpath('./@href').extract_first()
            yield scrapy.Request(url=detail_url, meta={'item': item}, callback=self.parse_info)


    def parse_info(self, response):
        print('ok')
        item = response.meta['item']


        yield item

结果无法打印ok字符, 也没有错误日志出现

打开调试模式,发现 [scrapy.spidermiddlewares.offsite] DEBUG: Filtered offsite request to 'album.zhenai.com': <GET http://album.zhenai.com/u/109625287>,原来是二次解析的域名被过滤掉了,解决办法

解决办法一:yield scrapy.Request(url=detail_url, meta={'item': item}, callback=self.parse_info, dont_filter=True)

原理:忽略allowed_domains的过滤

解决办法二: 将allowed_domains = ['www.zhenai.com']更改为allowed_domains = ['zhenai.com'] 即更换为对应的一级域名



  • 21
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值