代码之家  ›  专栏  ›  技术社区  ›  Hackaholic

python scrapy在控制台上看不到刮取的数据

  •  0
  • Hackaholic  · 技术社区  · 6 年前

    这是我的蜘蛛。

    import scrapy
    
    class HackaholicSpider(scrapy.Spider):
        name = "hackaholic"
    
        def start_request(self):
            urls = ['http://hackaholic.info']
            for url in urls:
                yield scrapy.Request(url=url, callback=self.parse)
    
        def parse(self, response):
            for article in response.xpath('//article'):
                yield {
                        'title': article.xpath("//h2[@class='entry-title']/a/text()").extract(),
                        'published_date': article.xpath("//div[@class='entry-meta']/span/time[@class='onDate date published']/a/text()").extract()
                       }
    

    我试图提取文章标题和发表日期。

    当我运行蜘蛛时:

    juniour@hackaholic:~/git/scrapy/tutorial$ scrapy crawl hackaholic
    2018-09-27 17:43:07 [scrapy.utils.log] INFO: Scrapy 1.5.1 started (bot: tutorial)
    2018-09-27 17:43:07 [scrapy.utils.log] INFO: Versions: lxml 4.2.1.0, libxml2 2.9.4, cssselect 1.0.3, parsel 1.5.0, w3lib 1.19.0, Twisted 18.7.0, Python 3.6.6 (default, Sep 12 2018, 18:26:19) - [GCC 8.0.1 20180414 (experimental) [trunk revision 259383]], pyOpenSSL 17.5.0 (OpenSSL 1.1.0g  2 Nov 2017), cryptography 2.1.4, Platform Linux-4.15.0-34-generic-x86_64-with-Ubuntu-18.04-bionic
    2018-09-27 17:43:07 [scrapy.crawler] INFO: Overridden settings: {'BOT_NAME': 'tutorial', 'NEWSPIDER_MODULE': 'tutorial.spiders', 'ROBOTSTXT_OBEY': True, 'SPIDER_MODULES': ['tutorial.spiders']}
    2018-09-27 17:43:07 [scrapy.middleware] INFO: Enabled extensions:
    ['scrapy.extensions.corestats.CoreStats',
     'scrapy.extensions.telnet.TelnetConsole',
     'scrapy.extensions.memusage.MemoryUsage',
     'scrapy.extensions.logstats.LogStats']
    2018-09-27 17:43:07 [scrapy.middleware] INFO: Enabled downloader middlewares:
    ['scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware',
     'scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
     'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
     'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
     'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
     'scrapy.downloadermiddlewares.retry.RetryMiddleware',
     'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
     'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
     'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
     'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
     'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
     'scrapy.downloadermiddlewares.stats.DownloaderStats']
    2018-09-27 17:43:07 [scrapy.middleware] INFO: Enabled spider middlewares:
    ['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
     'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
     'scrapy.spidermiddlewares.referer.RefererMiddleware',
     'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
     'scrapy.spidermiddlewares.depth.DepthMiddleware']
    2018-09-27 17:43:07 [scrapy.middleware] INFO: Enabled item pipelines:
    []
    2018-09-27 17:43:07 [scrapy.core.engine] INFO: Spider opened
    2018-09-27 17:43:07 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
    2018-09-27 17:43:07 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6024
    2018-09-27 17:43:07 [scrapy.core.engine] INFO: Closing spider (finished)
    2018-09-27 17:43:07 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
    {'finish_reason': 'finished',
     'finish_time': datetime.datetime(2018, 9, 27, 12, 13, 7, 699046),
     'log_count/DEBUG': 1,
     'log_count/INFO': 7,
     'memusage/max': 51904512,
     'memusage/startup': 51904512,
     'start_time': datetime.datetime(2018, 9, 27, 12, 13, 7, 696301)}
    2018-09-27 17:43:07 [scrapy.core.engine] INFO: Spider closed (finished)
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Valdir Stumm Junior    6 年前

    你必须重新命名 start_request start_requests ,这是必须从基中重写的方法 Spider here .

    所发生的事情是,你的蜘蛛没有发出任何请求,因为失踪 方法。

    应该是这样的:

    import scrapy
    
    class HackaholicSpider(scrapy.Spider):
        name = "hackaholic"
    
        def start_requests(self):
            urls = ['http://hackaholic.info']
            for url in urls:
                yield scrapy.Request(url=url, callback=self.parse)
    
        ...