【问题标题】:PhantomJS page dump script issuePhantomJS 页面转储脚本问题
【发布时间】:2013-07-16 02:46:00
【问题描述】:

Digikey 更改了他们的网站,现在有了一个名为 onload via post 的 javascript。这杀死了我以前的简单 java HTML 代码检索器。我正在尝试使用 PhantomJS 在保存 HTML/文本之前允许执行 javascript。

var page = new WebPage(),
t, address;


var fs = require('fs');

if (phantom.args.length === 0) {

console.log('Usage: save.js <some URL>');
phantom.exit();
} else {

address = encodeURI(phantom.args[0]);
page.open(address, function (status) {
    if (status !== 'success') {
        console.log('FAIL to load the address');
    } else {
        f = null;
        var markup = page.content;
        console.log(markup);
        try {
        f = fs.open('htmlcode.txt', "w");
        f.write(markup);
        f.close();          
        } catch (e) {
            console.log(e);
        }
    }   
    phantom.exit();

});

}

此代码适用于大多数网页,但在以下情况下失败:

http://search.digikey.com/scripts/dksearch/dksus.dll?keywords=S7072-ND

这是我的测试用例。它无法打开 URL,然后 PhantomJS 崩溃。使用 win32 静态构建 1.3。

有什么建议吗?

基本上我所追求的是 wget,它与页面渲染和脚本在保存文件之前修改文档竞争。

【问题讨论】:

    标签: javascript wget phantomjs


    【解决方案1】:

    一个快速的肮脏解决方案......但在phantomjs网站上发布......是使用超时。我已修改您的代码以包含 2 秒的等待时间。这允许页面在将内容转储到文件之前加载 2 秒。如果您需要精确的秒数,或者时间量会有很大差异,这个解决方案可能不适合您。

    var page = new WebPage(),
    
    t, address;
    
    
    var fs = require('fs');
    
    if (phantom.args.length === 0) {
    
    console.log('Usage: save.js <some URL>');
    phantom.exit();
    } else {
    
    address = encodeURI(phantom.args[0]);
    page.open(address, function (status) {
        if (status !== 'success') {
            console.log('FAIL to load the address');
        } else {
             window.setTimeout(function(){
                f = null;
                var markup = page.content;
                console.log(markup);
                try {
                f = fs.open('htmlcode.txt', "w");
                f.write(markup);
                f.close();          
                } catch (e) {
                    console.log(e);
                }
            }   
            phantom.exit();
        },2000);
    });
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-11
      • 1970-01-01
      • 1970-01-01
      • 2012-04-16
      • 2013-04-24
      • 2015-11-18
      • 1970-01-01
      相关资源
      最近更新 更多