Scrapy选择器不能处理Splash响应

时间:2016-06-08 11:54:20

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

我试图使用Scrapy抓取一些动态内容。 我成功地设置了Splash与它一起工作。 但是,以下蜘蛛的选择器会产生空结果:

# -*- coding: utf-8 -*- 

import scrapy
from scrapy.selector import Selector
from scrapy_splash import SplashRequest

class CartierSpider(scrapy.Spider):
  name = 'cartier'
  start_urls = ['http://www.cartier.co.uk/en-gb/collections/watches/mens-watches/ballon-bleu-de-cartier/w69017z4-ballon-bleu-de-cartier-watch.html']

  def start_requests(self):
    for url in self.start_urls:
      yield SplashRequest(url, self.parse, args={'wait': 0.5})

  def parse(self, response):
    yield {
      'title': response.xpath('//title').extract(),
      'link': response.url,
      'productID': Selector(text=response.body).xpath('//span[@itemprop="productID"]/text()').extract(),
      'model': Selector(text=response.body).xpath('//span[@itemprop="model"]/text()').extract(),
      'price': Selector(text=response.body).css('div.price-wrapper').xpath('.//span[@itemprop="price"]/text()').extract(),
    }

选择器使用Scrapy shell工作得很好,所以我对什么不起作用感到非常困惑。

我可以在两种情况中找到的唯一区别是字符串response.body的编码被区别对待:如果我尝试从{{1}内打印/解码它,那只是乱码功能。

非常感谢任何提示或参考。

3 个答案:

答案 0 :(得分:3)

您的蜘蛛可以正常使用,使用Scrapy 1.1,Splash 2.1并且不会修改您的问题中的代码,只需使用https://github.com/scrapy-plugins/scrapy-splash中建议的设置

正如其他人所提到的,您可以直接使用parseresponse.css()来简化response.xpath()功能,而无需从响应中重新构建Selector。 / p>

我尝试过:

import scrapy
from scrapy.selector import Selector
from scrapy_splash import SplashRequest

class CartierSpider(scrapy.Spider):
  name = 'cartier'
  start_urls = ['http://www.cartier.co.uk/en-gb/collections/watches/mens-watches/ballon-bleu-de-cartier/w69017z4-ballon-bleu-de-cartier-watch.html']

  def start_requests(self):
    for url in self.start_urls:
      yield SplashRequest(url, self.parse, args={'wait': 0.5})

  def parse(self, response):
    yield {
      'title': response.xpath('//title/text()').extract_first(),
      'link': response.url,
      'productID': response.xpath('//span[@itemprop="productID"]/text()').extract_first(),
      'model': response.xpath('//span[@itemprop="model"]/text()').extract_first(),
      'price': response.css('div.price-wrapper').xpath('.//span[@itemprop="price"]/text()').extract_first(),
    }

得到了这个:

$ scrapy crawl cartier
2016-06-08 17:16:08 [scrapy] INFO: Scrapy 1.1.0 started (bot: stack37701774)
2016-06-08 17:16:08 [scrapy] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'stack37701774.spiders', 'SPIDER_MODULES': ['stack37701774.spiders'], 'BOT_NAME': 'stack37701774'}
(...)
2016-06-08 17:16:08 [scrapy] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
 'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
 'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
 'scrapy.downloadermiddlewares.retry.RetryMiddleware',
 'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
 'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
 'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
 'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
 'scrapy_splash.SplashCookiesMiddleware',
 'scrapy_splash.SplashMiddleware',
 'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
 'scrapy.downloadermiddlewares.chunked.ChunkedTransferMiddleware',
 'scrapy.downloadermiddlewares.stats.DownloaderStats']
2016-06-08 17:16:08 [scrapy] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
 'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
 'scrapy.spidermiddlewares.referer.RefererMiddleware',
 'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
 'scrapy.spidermiddlewares.depth.DepthMiddleware']
2016-06-08 17:16:08 [scrapy] INFO: Enabled item pipelines:
[]
2016-06-08 17:16:08 [scrapy] INFO: Spider opened
2016-06-08 17:16:08 [scrapy] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2016-06-08 17:16:08 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023
2016-06-08 17:16:11 [scrapy] DEBUG: Crawled (200) <GET http://www.cartier.co.uk/en-gb/collections/watches/mens-watches/ballon-bleu-de-cartier/w69017z4-ballon-bleu-de-cartier-watch.html via http://localhost:8050/render.html> (referer: None)
2016-06-08 17:16:11 [scrapy] DEBUG: Scraped from <200 http://www.cartier.co.uk/en-gb/collections/watches/mens-watches/ballon-bleu-de-cartier/w69017z4-ballon-bleu-de-cartier-watch.html>
{'model': u'Ballon Bleu de Cartier watch', 'productID': u'W69017Z4', 'link': 'http://www.cartier.co.uk/en-gb/collections/watches/mens-watches/ballon-bleu-de-cartier/w69017z4-ballon-bleu-de-cartier-watch.html', 'price': None, 'title': u'CRW69017Z4 - Ballon Bleu de Cartier watch - 36 mm, steel, leather - Cartier'}
2016-06-08 17:16:11 [scrapy] INFO: Closing spider (finished)
2016-06-08 17:16:11 [scrapy] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 618,
 'downloader/request_count': 1,
 'downloader/request_method_count/POST': 1,
 'downloader/response_bytes': 213006,
 'downloader/response_count': 1,
 'downloader/response_status_count/200': 1,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2016, 6, 8, 15, 16, 11, 201281),
 'item_scraped_count': 1,
 'log_count/DEBUG': 3,
 'log_count/INFO': 7,
 'response_received_count': 1,
 'scheduler/dequeued': 2,
 'scheduler/dequeued/memory': 2,
 'scheduler/enqueued': 2,
 'scheduler/enqueued/memory': 2,
 'splash/render.html/request_count': 1,
 'splash/render.html/response_count/200': 1,
 'start_time': datetime.datetime(2016, 6, 8, 15, 16, 8, 545105)}
2016-06-08 17:16:11 [scrapy] INFO: Spider closed (finished)

答案 1 :(得分:2)

我已经尝试了SplashRequest并遇到了和你一样的问题。搞乱之后,我尝试执行LUA脚本。

script = """
                function main(splash)
  local url = splash.args.url
  assert(splash:go(url))
  assert(splash:wait(0.5))
  return {
    html = splash:html(),
    png = splash:png(),
    har = splash:har(),
  }
end
"""

然后使用脚本作为参数发出请求。你可以乱用脚本。在localhost:9200或您选择的其他端口上测试它。

yield SplashRequest(
            url,
            self.parse, args={'lua_source': self.script}, endpoint='execute')

哦,顺便说一句,你产生信息的方式很奇怪,而是使用项目。

答案 2 :(得分:1)

我没有足够的声誉来添加评论,所以我必须在这里做出答案。

如果我为SplashRequest设置'Accept-Encoding': 'gzip',我将面临一个类似的问题,即Splash 2.1返回&#34;格式错误&#34;(未压缩的gziped实际发言)html内容。

最后,我在这里找到了解决方案:https://github.com/scrapinghub/splash/pull/102'Accept-Encoding': 'gzip'更改为: 'Accept-Encoding': 'deflate'

我不知道为什么,但它确实有效。