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

标签 javascript linux download wget web-testing

<分区>

如何在 Linux 中下载网站副本?

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

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

最佳答案

幻影.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();
});

关于javascript - 如何下载包含javascript代码查找结果的网站?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48636738/

相关文章:

python - 我可以用python下载和分割视频吗?

javascript - 在 JavaScript 中从字符串数组中创建分组对象

javascript - Qooxdoo 布局问题

javascript - Angularjs:ng-if通过比较字符串显示内容

linux - 刷新CPU缓存而不使缓存失效?

http - 使用 curl 或 wget 命令行下载文件

javascript - 如何将 AngularJS 变量绑定(bind)为 Polymer 全局变量?

linux - 脚本不会退出

PHP 或 Linux Shell : Fastest way to reduce a text file that has more than 10 lines to only have the last 10 lines

c# - WebAPI 返回文件和下载未开始