zoukankan      html  css  js  c++  java
  • [scrapy.spidermiddlewares.offsite] 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'] 即更换为对应的一级域名

  • 相关阅读:
    FastDFS
    目前存在的问题
    MongoDB JAVA开发
    [Linux] Hexo 搭建个人博客
    新目标
    1年之后的拿高工资的资本,Java线程
    Oracle在VMware虚拟机安装的配置
    adb命令关闭打开手机wifi开关
    ADB命令横竖屏切换、关闭打开wifi
    使用adb命令提取安卓手机中安装的apk
  • 原文地址:https://www.cnblogs.com/xiaohuhu/p/12257344.html
Copyright © 2011-2022 走看看