【问题标题】:Issue with scraping JS rendered page with Scrapy and Splash使用 Scrapy 和 Splash 抓取 JS 渲染页面的问题
【发布时间】:2017-08-31 08:04:31
【问题描述】:

我正在尝试根据 chrome 抓取 this page,其中包括以下 html

<p class="title">

            Orange Paired

        </p>

这是我的蜘蛛:

import scrapy
from scrapy_splash import SplashRequest

class MySpider(scrapy.Spider):
    name = "splash"
    allowed_domains = ["phillips.com"]
    start_urls = ["https://www.phillips.com/detail/BRIDGET-RILEY/UK010417/19"]
    def start_requests(self):
        for url in self.start_urls:
            yield SplashRequest(
                url,
                self.parse,
                endpoint='render.json',
                args={'har': 1, 'html': 1}
            )
    def parse(self, response):
        print("1. PARSED", response.real_url, response.url)
        print("2. ",response.css("title").extract())
        print("3. ",response.data["har"]["log"]["pages"])
        print("4. ",response.headers.get('Content-Type'))
        print("5. ",response.xpath('//p[@class="title"]/text()').extract())

这是scrapy runspider spiders/splash_spider.py的输出

2017-08-31 09:48:18 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
1. PARSED http://localhost:8050/render.json https://www.phillips.com/detail/BRIDGET-RILEY/UK010417/19
2.  ['<title>PHILLIPS : Bridget Riley, Orange Paired</title>', '<title>Page 1</title>']
3.  [{'title': 'PHILLIPS : Bridget Riley, Orange Paired', 'pageTimings': {'onContentLoad': 3832, '_onStarted': 1, '_onIframesRendered': 4667, 'onLoad': 4664, '_onPrepareStart': 4664}, 'id': '1', 'startedDateTime': '2017-08-31T07:48:18.986240Z'}]
4.  b'text/html; charset=utf-8'
5.  []
2017-08-31 09:48:23 [scrapy.core.engine] INFO: Closing spider (finished)

为什么我得到一个 5 的空输出?

我也不明白的是 Splash 似乎没有呈现上面链接的页面

但它呈现顶级主页

【问题讨论】:

    标签: javascript python-3.x web-scraping scrapy splash-screen


    【解决方案1】:

    在这种情况下,好的起点是查看 Splash 文档的 FAQ 部分。事实证明,在您的情况下,您需要为 Splash 设置 disable Private mode,或者通过 Docker 的 --disable-private-mode 启动选项,或者在 LUA 脚本中设置 splash.private_mode_enabled = false

    禁用隐私模式后,页面将正确呈现。

    【讨论】:

    • 我用--disable-private-mode 启动了 Docker,它成功了。非常感谢
    猜你喜欢
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 2021-08-16
    • 1970-01-01
    • 2018-07-28
    • 2017-08-20
    • 2017-03-08
    • 1970-01-01
    相关资源
    最近更新 更多