javascript - 如何使用 phantomjs 下载包含所有相关内容的网页

标签 javascript phantomjs

我想下载整个网页以及使用 PhantomJS 在任何浏览器中呈现网页可能需要的内容(包括 CSS、JavaScript、图像、外部资源)。我不想执行脚本,只是解析 CSS 和 JavaScript 以获得更多内容链接并下载它们。

我尝试使用像 wget 这样的工具(完全满足我的需要,但速度很慢,因为它使用到网络服务器的单个 tcp 连接)和 httrack(下载整个网站,在我的例子中,我只想下载需要的内容通过递归解析文件中的链接来呈现页面)。我目前正在为此目的尝试使用 phantomjs,但找不到正确的使用方法。

最佳答案

尝试使用这段代码:

var page = require('webpage').create();

var url = "your url goes here";
var fs = require('fs');
var path = 'index.html';//you might want to change format whether .json .txt etc.

page.open(url, function (status) {
    if(status !== 'success')
        console.log('Connection failed, page was not loaded!');
    else
        var content = page.content;
        fs.write(path, content ,'w')
        phantom.exit();
});

这必须为您提供网页的全部内容。如果您需要进一步的帮助,请告诉我!

关于javascript - 如何使用 phantomjs 下载包含所有相关内容的网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41124056/

相关文章:

node.js - 我在 Azure 上有这些模块,为什么我收到 Recipe phantom-pdf was not found.?

javascript - 在 html 中显示嵌套的 json 对象

javascript - 在jsp中检查select中的onchange值后重置值

javascript - 生成PDF时如何检测长表的分页符

javascript - phantomJS JavaScript

ruby - VCR代理 : Record PhantomJS ajax calls with VCR inside Capybara

phantomjs - 如何使用 karma 和 phantomjs 解决内存错误

javascript - 语言独立 : split a line with multiple repeating properties into multiple lines with each property only once

javascript - jQuery html() 未完成

testing - 使用 Laika 和 PhantomJS 进行集成测试