PhantomJS page dump script issue

ぐ巨炮叔叔 提交于 2020-02-02 02:20:12

问题


Digikey has changed their website and now has a javascript that is called onload via post. This killed my former simple java HTML code retriever. I am trying to use PhantomJS to allow the execution of the javascript before saving the HTML/text.

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();

});

}

This code works with most webpages but fails on:

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

Which is my test case. It fails to open the URL and then PhantomJS crashes. Using win32 static build 1.3.

Any tips?

Basically what I am after is wget that competes the page rendering and scripts that modify the document before saving the file.


回答1:


a quick an dirty solution... and yet is posted on the phantomjs site... is to use a time out. I have modified your code to include a 2 second wait. this allows the page to load for 2 seconds before dumping the contents to a file. If you need the exact second or the amount of time will vary greatly this solution probably wont work for you.

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);
});

}


来源:https://stackoverflow.com/questions/8692038/phantomjs-page-dump-script-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!