【问题标题】:Issues scraping dynamic site (PhantomJS)问题抓取动态站点(PhantomJS)
【发布时间】:2019-12-26 19:36:23
【问题描述】:

我正在尝试寻找一种方法来获取/下载website

我尝试了wgetcurl 但没有成功,然后我被引导到PhantomJS

var url = 'https://www.sagedining.com/menus/admiralfarragutacademy';
var fs = require('fs');
var page = require('webpage').create();
page.open(url, function(status) {
    if (status === 'success') {
        var html = page.evaluate(function() {
            return document.documentElement.outerHTML;
        });
        try {
            fs.write("/root/choate/page.html", html, 'w');
        } catch(e) {
            console.log(e);
        }
    }
    phantom.exit();
});

当我在我的 Debian VPS 上运行此代码时,

sudo xvfb-run -- phantomjs menu.js

它会在网站仍在加载时下载该网站,因此只下载加载屏幕。 每次运行时也会抛出这个错误:

TypeError: Attempting to change the setter of an unconfigurable property.

TypeError: Attempting to change the setter of an unconfigurable property.

在加载所有菜单后,有什么方法可以下载这个网站?报错信息和它有关系吗?

提前谢谢你。

【问题讨论】:

  • 我不熟悉 phantomjs,但你为什么不等待几秒钟 setTimeout ,以确保 web 已加载?类似于主要示例:phantomjs.org,如果您不想等待固定秒数,您可以使用setInterval 检查页面数据是否已完全加载。

标签: javascript curl phantomjs wget


【解决方案1】:

该错误来自 PhantomJS,因为页面代码试图在 DOM 中设置一些属性,并且可能无法访问它们。您应该等待加载发生,您可以使用超时功能来完成:

   if (status === 'success') {
   window.setTimeout(function () {
        var html = page.evaluate(function() {
        return document.documentElement.outerHTML;
    });
    try {
          fs.write("/root/choate/page.html", html, 'w');
        } catch(e) {
          console.log(e);
        }
    }, 1000); //Increase the value if you need more time
   }      

【讨论】:

  • 很抱歉,您正在抓取的网页似乎与 PhantomJS 引擎存在不兼容问题,也许有办法绕过该错误。
  • 尽管超时时间为 15000 毫秒,但实际等待似乎并不需要 15 秒。
  • 是的,您遇到的问题不是因为超时。它永远不会加载,因为它由于某些 PhantomJS 中不允许的 JS 功能而失败。在PhantomJS forum上报告了一些关于它的Bug
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-20
  • 1970-01-01
  • 1970-01-01
  • 2010-09-17
  • 1970-01-01
相关资源
最近更新 更多