Scrapy 爬行器未执行回调

Scrapy 爬行器未执行回调,scrapy,web-crawler,scrapy-spider,Scrapy,Web Crawler,Scrapy Spider,我通过扩展爬行蜘蛛创建了一个蜘蛛 当spider运行并找到文章页面时,我想获得一个指向authors概要文件的链接,并向概要文件页面发出请求,并使用parse_author对其进行解析,但由于某些原因,这个parse_author回调从未执行过 我的代码: import scrapy from scrapy.spiders import CrawlSpider, Rule from scrapy.linkextractors import LinkExtractor from scrapy.ht

我通过扩展爬行蜘蛛创建了一个蜘蛛

当spider运行并找到文章页面时,我想获得一个指向authors概要文件的链接,并向概要文件页面发出请求,并使用parse_author对其进行解析,但由于某些原因,这个parse_author回调从未执行过

我的代码:

import scrapy
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
from scrapy.http.request import Request


class CityamSpider4(CrawlSpider):

    name = "city_am_v4"
    custom_settings = {
        'CONCURRENT_REQUESTS': '1',
    }
    allowed_domains = ['cityam.com']
    start_urls = [
        'http://www.cityam.com',
    ]
    rules = (
        Rule(LinkExtractor(deny=('dev2.cityam.com', 'sponsored-content', )), callback='parse_item'),
    )

    def parse_item(self, response):
        # parse article page
        article_title = response.css('.article-headline h1::text').extract_first(default='null').strip()
        if article_title is not 'null':
            print 'Article url : ' + response.url
            author_url = response.css('.author-container .author-text a.author-name::attr(href)').extract_first(default='null').strip()
            print 'Author link: ' + author_url
            author_url = response.urljoin(author_url)
            print 'Author link: ' + author_url
            yield Request(author_url, callback=self.parse_author)

    def parse_author(self, response):
        # parse author page
        author_name = response.css(".cam-profile-header-title::text").extract_first(default='null').strip()
        print 'Author name: ' + author_name
        yield {
            'name': author_name,
        }

您在站点页面上使用了错误的端口:

  • HTTP:80端口-
  • HTTPS:443端口-
  • 您的行
    生成请求(url=author\u url,callback=self.parse\u author)
    引用(例如)http://www.cityam.com/profile/joseph.ray(在端口80上)

    您必须通过HTTPS:HTTPS://www.cityam.com/profile/joseph.ray使用请求 然后程序执行将继续执行方法parse\u author

    更改您的请求URL,每个人都会很高兴。 顺便说一下,我认为编写这样的代码是一个错误的做法:

    print'Article url:'+response.url
    (一些编译器不理解这一点,并且会出现错误。)


    必须是:
    打印(“文章url:+response.url)

    问题是链接提取规则也与作者链接匹配,默认情况下,Scrapy会删除重复的请求,因此您的
    parse_item
    方法将接收您在
    parse_author
    中期望的响应

    可能的解决办法包括:

    • 修改您的
      LinkExtractor
      以使其与作者URL不匹配

    • 将作者解析逻辑从
      parse_author
      移动到
      parse_item

    • dont_filter=True
      添加到
      Request
      中,这样,即使是链接提取器找到的请求的副本,您正在生成的请求也不会被过滤掉


    这太棒了,我浪费了3天的培训来解决这个问题:)你能解释一下为什么我需要使用https端口,即使站点可以通过http端口访问吗?再次感谢。我刚刚用在线扫描仪扫描了所有可能的端口。而且不难猜测这个站点将使用https,因为信息应该受到保护。未显示:997关闭端口端口状态服务22/tcp打开ssh 80/tcp打开http 443/tcp打开https Nmap完成:1个IP地址(1个主机向上)在10.24秒内扫描正如您可能猜到的,使用https的一个关键因素是安全性。它最大限度地防止从设备到服务器的过程中数据被盗。这不是问题所在。关于
    print
    的评论不合适,用户代码显然是Python 2,而那些print语句在Python 2中非常好。在我看来,Python版本不会随着时间的推移而回滚,谁使用的是版本2?只有Gallaecio。也许他是对的。回到Python2,您会很高兴的