scrapy-splash活动内容选择器在shell中工作,但不在spider中工作

时间:2018-06-16 00:56:01

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

我刚刚开始使用scrapy-splash来检索opentable.com的预订数量。以下在shell中工作正常:

$ scrapy shell 'http://localhost:8050/render.html?url=https://www.opentable.com/new-york-restaurant-listings&timeout=10&wait=0.5'    
...

In [1]: response.css('div.booking::text').extract()
Out[1]: 
['Booked 59 times today',
 'Booked 20 times today',
 'Booked 17 times today',
 'Booked 29 times today',
 'Booked 29 times today',
  ... 
]

但是,这个简单的蜘蛛会返回一个空列表:

class TableSpider(scrapy.Spider):
    name = 'opentable'
    start_urls = ['https://www.opentable.com/new-york-restaurant-listings']

    def start_requests(self):
        for url in self.start_urls:
            yield SplashRequest(url=url,
                                callback=self.parse,
                                endpoint='render.html',
                                args={'wait': 1.5},
                                )

    def parse(self, response):
        yield {'bookings': response.css('div.booking::text').extract()}
用以下方式调用

$ scrapy crawl opentable
...
DEBUG: Scraped from <200 https://www.opentable.com/new-york-restaurant-listings>
{'bookings': []}

我已经尝试过未成功

docker run -it -p 8050:8050 scrapinghub/splash --disable-private-mode

并增加了等待时间。

2 个答案:

答案 0 :(得分:3)

我认为您的问题出在middlewares,首先您需要添加一些设置

# settings.py

# uncomment `DOWNLOADER_MIDDLEWARES` and add this settings to it
DOWNLOADER_MIDDLEWARES = {
    'scrapy_splash.SplashCookiesMiddleware': 723,
    'scrapy_splash.SplashMiddleware': 725,
    'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware': 810,
}

# url of splash server
SPLASH_URL = 'http://localhost:8050'

# and some splash variables
DUPEFILTER_CLASS = 'scrapy_splash.SplashAwareDupeFilter'
HTTPCACHE_STORAGE = 'scrapy_splash.SplashAwareFSCacheStorage'

现在运行docker

sudo docker run -it -p 8050:8050 scrapinghub/splash --disable-private-mode

如果我执行所有这些步骤,请回来:

scrapy crawl opentable

...

2018-06-23 11:23:54 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.opentable.com/new-york-restaurant-listings via http://localhost:8050/render.html> (referer: None)
2018-06-23 11:23:54 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.opentable.com/new-york-restaurant-listings>
{'bookings': [
    'Booked 44 times today',
    'Booked 24 times today',
    'and many others Booked values'
]}

答案 1 :(得分:0)

这不起作用,因为网络上的该内容正在使用JS。

您可以采用服务器解决方案:

1)使用硒。

2)如果看到页面的API,则调用此网址<GET https://www.opentable.com/injector/stats/v1/restaurants/<restaurant_id>/reservations>,将获得该特定餐厅的当前预订数量(restaurant_id)。