python-3.x - 抓取 + 飞溅 : not rendering full page javascript data

标签 python-3.x scrapy scrapy-splash

我只是在用 splash 探索 scrapy,我正试图从一个电子商务网站上抓取所有带有产品 ID、名称和价格的产品(裤子)数据 gap但是当我从 splash web UI 查看时,我没有看到所有动态产品数据加载 splash web UI (虽然每个请求只加载 16 个项目 - 不知道为什么) 我尝试了以下选项,但没有成功

  • 将等待时间增加到 20 秒
  • 通过使用“--disable-private-mode”启动 docker
  • 使用lua_script实现页面滚动
  • 使用 View 报告完整选项 splash:set_viewport_full()

lua_script2 = """ function main(splash)
    local num_scrolls = 10
    local scroll_delay = 2.0

    local scroll_to = splash:jsfunc("window.scrollTo")
    local get_body_height = splash:jsfunc(
        "function() {return document.body.scrollHeight;}"
    )
    assert(splash:go(splash.args.url))
    splash:wait(splash.args.wait)

    for _ = 1, num_scrolls do
        scroll_to(0, get_body_height())
        splash:wait(scroll_delay)
    end        
    return splash:html()
end"""                 
                              
            yield SplashRequest(
                url,
                self.parse_product_contents,
                endpoint='execute', 
                args={
                        'lua_source': lua_script2,
                        'wait': 5,
                    }
                )
 

任何人都可以阐明这种行为吗? p.s:我正在使用 scrapy 框架,我能够从 render.html 中解析产品信息(itemid,名称和价格)(但 render.html 只有 16 个项目信息)

最佳答案

我将脚本更新到下面

function main(splash)
    local num_scrolls = 10
    local scroll_delay = 2.0
    splash:set_viewport_size(1980, 8020)
    local scroll_to = splash:jsfunc("window.scrollTo")
    local get_body_height = splash:jsfunc(
        "function() {return document.body.scrollHeight;}"
    )
    assert(splash:go(splash.args.url))
--    splash:set_viewport_full()
    splash:wait(10)
    splash:runjs("jQuery('span.icon-x').click();")
    splash:wait(1)
    for _ = 1, num_scrolls do
        scroll_to(0, get_body_height())
        splash:wait(scroll_delay)
    end      

      splash:wait(30)

    return { 
        png = splash:png(),
        html = splash:html(),
        har = splash:har()
       }
end

然后在我的本地 splash 中运行它,png 不能正常工作,但 HTML 有最后一个产品

Last Image on page

Splash Rendered HTML

唯一的问题是当电子邮件订阅弹出窗口出现时它不会滚动,所以我添加了代码来关闭它

关于python-3.x - 抓取 + 飞溅 : not rendering full page javascript data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46058568/

相关文章:

python - 如何使用 BeautifulSoup 发送 key

python - scrapy:使用 xpath 从 div 中删除一些元素 span 元素

mongodb - 使用 pymongo 的 MongoDB 的 dateFromString 运算符的问题

Python3 + pytest + pytest 模拟 : Mocks leaking into other test functions breaking assertions?

python - 无法强制 scrapy 使用重定向的 url 进行回调

python - Scrapy中如何获取上层函数的url地址?

javascript - scrapy + 飞溅 : trying to scrape website with ajax calls and javascript

python - Scrapy/Splash 单击按钮,然后从新窗口中的新页面获取内容

python - 如何统计文本文件中重复单词的数量?