报纸上的scrat extrat到txt

时间:2017-05-03 10:43:56

标签: scrapy scrapy-spider

我对scrapy有点新意,我需要为一件作品提取一些报纸信息,我已经尝试了一些教程,但没有一个像我预期的那样工作,目标是给定一个url ,提取有关前4个主题的信息(点击链接时的内部信息)。 我首先试图浏览链接,我失败了,输出是空的并且说0抓取的页面。

import scrapy

class BlogSpider(scrapy.Spider):
    name = 'blogspider'
    start_urls = ['http://www.dn.pt/pesquisa.html?q=economia%20empresas']

    def parse(self, response):
        for title in response.css('div.gs-title'):
            yield 
            {
            'title': title.css('a.gs-title::text').extract_first()
            }

the title / link for the first news

the div containing all the news

Crawler转到第一个链接,然后提取该链接的信息,然后转到第二个链接,并提取第二个链接的信息。就像第4或第5条新闻一样。

记录:

2017-05-03 12:46:37 [scrapy.utils.log] INFO: Scrapy 1.3.3 started (bot: scrapybot)
2017-05-03 12:46:37 [scrapy.utils.log] INFO: Overridden settings: {'SPIDER_LOADER_WARN_ONLY': True, 'LOG_FILE': 'output.log'}
2017-05-03 12:46:37 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.logstats.LogStats',
 'scrapy.extensions.telnet.TelnetConsole',
 'scrapy.extensions.corestats.CoreStats']
2017-05-03 12:46:37 [scrapy.middleware] INFO: Enabled downloader middlewares:
['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.stats.DownloaderStats']
2017-05-03 12:46:37 [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']
2017-05-03 12:46:37 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2017-05-03 12:46:37 [scrapy.core.engine] INFO: Spider opened
2017-05-03 12:46:37 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2017-05-03 12:46:37 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023
2017-05-03 12:46:38 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://www.dn.pt/pesquisa.html?q=economia%20empresas> (referer: None)
2017-05-03 12:46:38 [scrapy.core.engine] INFO: Closing spider (finished)
2017-05-03 12:46:38 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 242,
 'downloader/request_count': 1,
 'downloader/request_method_count/GET': 1,
 'downloader/response_bytes': 16289,
 'downloader/response_count': 1,
 'downloader/response_status_count/200': 1,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2017, 5, 3, 11, 46, 38, 199050),
 'log_count/DEBUG': 2,
 'log_count/INFO': 7,
 'response_received_count': 1,
 'scheduler/dequeued': 1,
 'scheduler/dequeued/memory': 1,
 'scheduler/enqueued': 1,
 'scheduler/enqueued/memory': 1,
 'start_time': datetime.datetime(2017, 5, 3, 11, 46, 37, 873062)}
2017-05-03 12:46:38 [scrapy.core.engine] INFO: Spider closed (finished)

1 个答案:

答案 0 :(得分:1)

我快速浏览了update quiz set maxscore = scr.maxscore from ( select quizid, sum(maxscore) as maxscore from ( select quizid, max(a.answermark) as maxscore from answer a inner join questions q using (questionid) group by quizid, questionid ) s group by quizid ) scr where quiz.quizid = scr.quizid ,似乎内容没有附带scrapy捕获的初始HTML。

相反,内容是由后续的Javascript / AJAX请求下载和呈现的,Scrapy没有开箱即用。

可能的解决方案:

您可以使用Firebug或Chrome开发者工具来了解这些后台请求的工作方式,并尝试直接模拟和抓取这些后台请求。 (意味着更多的工作,但最终的刮刀更快)。

或者您添加Splash或Selenium实例以使它们呈现Javascript,然后直接刮取呈现的页面。