Phantomjs相当于浏览器的“页面另存为...,完整的网页"; [英] Phantomjs equivalent of browser's "Save Page As... Webpage, complete"
问题描述
对于我的应用程序,我需要以编程方式保存网页HTML的副本以及呈现它所需的图像和资源.浏览器的Save page as...
Webpage, complete
选项具有此功能.
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.
使用phantomjs或casperjs保存页面的呈现HTML当然很容易.但是,我还没有看到将其与下载关联的图像相结合,并进行必要的DOM更改以使用下载的图像的任何示例.
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.
鉴于此功能存在于基于Webkit的浏览器(Chrome,Safari)中,我很惊讶它不在phantomjs中-也许我只是没有找到它!
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!
推荐答案
或者使用PhantomJS,您可以使用CasperJS来获得所需的结果. CasperJS是基于PhantomJS的框架,但是具有支持和补充PhantomJS的各种模块和类.
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!
这篇关于Phantomjs相当于浏览器的“页面另存为...,完整的网页";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!