如何下载包含 javascript 代码查找结果的网站?

How to download a website where javascript code lookup results are included?

如何下载 linux 中的网站副本?

我试过使用 wget --recursive --level=inf https://example.com 但是它也从不同的域下载链接。

还有一种方法可以下载 javascript 具有 运行 并在页面上产生输出的网站副本。例如,如果下载一个天气网站,可能会有 javascript 在数据库中查找当前温度,然后呈现输出。如何捕获 temperature/final 输出?

Phantom.js?

http://phantomjs.org/quick-start.html

我想这会做你喜欢的事!

最好的办法是从这里安装:

http://phantomjs.org/

基本上,您 运行 通过创建 javascript 脚本并作为命令行参数传递,例如

phantomjs.exe someScript.js

有很多示例,您可以将网站呈现为图像, 例如你可以这样做:

phantomjs.exe github.js

github.js 看起来像

var page = require('webpage').create();
page.open('http://github.com/', function() {
  page.render('github.png');
  phantom.exit();
});

这个演示是在 http://phantomjs.org/screen-capture.html

您还可以将网页内容显示为文本。

举个简单的网页为例,demo_page.html:

<html>
    <head>
        <script>
        function setParagraphText() {
            document.getElementById("1").innerHTML = "42 is the answer.";
        }
        </script> 
    </head>
    <body onload="setParagraphText();">
        <p id="1">Static content</p>
    <body>
</html>

然后创建测试脚本,test.js:

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

page.open("demo_page.html", function(status) {
    console.log("Status: " + status);
    if(status === "success") {
        console.log('Page text' + page.plainText);
        console.log('All done');        
    }
phantom.exit();
});

然后在控制台写:

> phantomjs.exe test.js
Status: success
Page text: 42 is the answer.
All done

您还可以检查页面 DOM 甚至更新它:

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

page.open("demo_page.html", function(status) {
    console.log("Status: " + status);
    if(status === "success") {
        page.evaluate(function(){
            document.getElementById("1").innerHTML = "I updated the value myself";
        });

        console.log('Page text: ' + page.plainText);
        console.log('All done');
    }
    phantom.exit();
});