Phantomjs equivalent of browser's “Save Page As… Webpage, complete”

拟墨画扇 提交于 2019-12-09 23:19:57

问题


For my application I need to programmatically save a copy of a webpage HTML along with the images and resources needed to render it. Browsers have this functionality in their Save page as... Webpage, complete options.

It is of course easy to save the rendered HTML of a page using phantomjs or casperjs. However, I have not seen any examples of combining this with downloading the associated images, and doing the needed DOM changes to use the downloaded images.

Given that this functionality exists in webkit-based browsers (Chrome, Safari) I'm surprised it isn't in phantomjs -- or perhaps I just haven't found it!


回答1:


Alternatively the PhantomJS, you can use the CasperJS to achieve the required result. CasperJS is a framework based on the PhantomJS, however, with a variety of modules and classes that support and complement the PhantomJS.

An example of a script that you can use is:

casper.test.begin('test script', 0, function(test) {
    casper.start(url);

    casper.then(function myFunction(){
        //...
    });

    casper.run(function () {
        //...
        test.done();
    });
});

With this script, within a "step", you can perform your downloads, be it a single image of a document, the page, a print or whatever. Take a study on the download methods, getPageContent and capture / captureSelector in this link.

I hope these pointers can help you to go further!



来源:https://stackoverflow.com/questions/31775837/phantomjs-equivalent-of-browsers-save-page-as-webpage-complete

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