非常简单的Scrapy + Splash项目

时间:2017-11-18 09:51:47

标签: python web-scraping scrapy scrapy-spider scrapy-splash

我正在开发一个非常简单的Scrapy + Splash项目来抓取javascript网站。这是我的代码:

splashtest.py:

import scrapy
from scrapy_splash import SplashRequest

class SplashSpider(scrapy.Spider):
    name = 'splash_spider'

    def start_requests(self):
        yield SplashRequest(
            url='http://aszx.altervista.org/scrapytest/test.html',
            callback=self.parse,
       )

    def parse(self, response):
        self.log('I have just visited ' + response.url)
        for squadre in response.css('#hello'):
            yield {
                'name': squadre.css('::text').extract_first(),
            }   

settings.py:

BOT_NAME = 'emybet'

SPIDER_MODULES = ['emybet.spiders']
NEWSPIDER_MODULE = 'emybet.spiders'

SPLASH_URL = 'http://localhost:8050'
DOWNLOADER_MIDDLEWARES = {
    'scrapy_splash.SplashCookiesMiddleware': 723,
    'scrapy_splash.SplashMiddleware': 725,
    'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware': 810,
}
SPIDER_MIDDLEWARES = {
    'scrapy_splash.SplashDeduplicateArgsMiddleware': 100,
}
DUPEFILTER_CLASS = 'scrapy_splash.SplashAwareDupeFilter'

HTTPCACHE_STORAGE = 'scrapy_splash.SplashAwareFSCacheStorage'

这是我的回答:

# scrapy runspider splashtest.py
2017-11-18 10:10:21 [scrapy] INFO: Scrapy 1.0.5.post4+g4b324a8 started (bot: scrapybot)
2017-11-18 10:10:21 [scrapy] INFO: Optional features available: ssl, http11
2017-11-18 10:10:21 [scrapy] INFO: Overridden settings: {}
2017-11-18 10:10:21 [scrapy] INFO: Enabled extensions: CloseSpider, TelnetConsole, LogStats, CoreStats, SpiderState
2017-11-18 10:10:22 [scrapy] INFO: Enabled downloader middlewares: HttpAuthMiddleware, DownloadTimeoutMiddleware, UserAgentMiddleware, RetryMiddleware, DefaultHeadersMiddleware, MetaRefreshMiddleware, HttpCompressionMiddleware, RedirectMiddleware, CookiesMiddleware, ChunkedTransferMiddleware, DownloaderStats
2017-11-18 10:10:22 [scrapy] INFO: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddleware
2017-11-18 10:10:22 [scrapy] INFO: Enabled item pipelines: 
2017-11-18 10:10:22 [scrapy] INFO: Spider opened
2017-11-18 10:10:22 [scrapy] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2017-11-18 10:10:22 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023
2017-11-18 10:10:22 [scrapy] DEBUG: Crawled (200) <GET http://aszx.altervista.org/scrapytest/test.html> (referer: None)
2017-11-18 10:10:22 [splash_spider] DEBUG: I have just visited http://aszx.altervista.org/scrapytest/test.html
2017-11-18 10:10:22 [scrapy] DEBUG: Scraped from <200 http://aszx.altervista.org/scrapytest/test.html>
{'name': u'No Javascript'}
2017-11-18 10:10:22 [scrapy] INFO: Closing spider (finished)
2017-11-18 10:10:22 [scrapy] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 252,
 'downloader/request_count': 1,
 'downloader/request_method_count/GET': 1,
 'downloader/response_bytes': 392,
 'downloader/response_count': 1,
 'downloader/response_status_count/200': 1,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2017, 11, 18, 9, 10, 22, 975769),
 'item_scraped_count': 1,
 'log_count/DEBUG': 4,
 '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, 11, 18, 9, 10, 22, 469984)}
2017-11-18 10:10:22 [scrapy] INFO: Spider closed (finished)

所以我没有得到javascript加载的内容。 Splash正在运行,因为如果我将浏览器指向template metaprogramming,那么我会看到Splash页面。

那么,问题是什么?

由于

1 个答案:

答案 0 :(得分:1)

问题是我没有用命令启动scrapy项目:“scrapy startproject name”但我自己创建了文件夹和文件。