JavaScript到Html

JavaScript到Html,javascript,html,command-line-interface,html-generation,Javascript,Html,Command Line Interface,Html Generation,给定一个包含JavaScript代码的网页,我希望自动生成生成的html(通过CLI工具或使用某种语言的库) 例如,给定test.html <!DOCTYPE html> <html> <body> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = "Hello JavaScript!"; &l

给定一个包含JavaScript代码的网页,我希望自动生成生成的html(通过CLI工具或使用某种语言的库)

例如,给定
test.html

<!DOCTYPE html>
<html>
  <body>
    <p id="demo"></p>
    <script>
      document.getElementById("demo").innerHTML = "Hello JavaScript!";
    </script>
  </body>
</html>
var page = require('webpage').create(),
    system = require('system'),
    page_address;
var fs = require('fs');
if (system.args.length === 1){
  console.log('Usage: phantomjs ' + system.args[0] + ' <page_to_load:http://www.google.com>');
  phantom.exit();
}
page_address = system.args[1]

page.open(page_address, function(status){
    console.log('Status:' + status);
    if (status === 'success' ){
      fs.write('phantom_result.html', page.content, 'w')
    }
    phantom.exit();
});

document.getElementById(“demo”).innerHTML=“Hello JavaScript!”;
我想得到一个结果

<html>
  <body>
    <p id="demo">Hello JavaScript!</p>
    <script>
      document.getElementById("demo").innerHTML = "Hello JavaScript!";
    </script> 
  </body>
</html>

你好,JavaScript

document.getElementById(“demo”).innerHTML=“Hello JavaScript!”;
快速搜索后,它看起来会做你想做的事情


它的目标是自动测试,但当它点击一个页面时,它将执行所有的js以及ajax调用等。看起来你也可以从中获取生成的html。

答案基于@torazaburo的评论

事实上,可以评估javascript并生成html

下面是执行
phantomjs load_page.js path_to/test.html

<!DOCTYPE html>
<html>
  <body>
    <p id="demo"></p>
    <script>
      document.getElementById("demo").innerHTML = "Hello JavaScript!";
    </script>
  </body>
</html>
var page = require('webpage').create(),
    system = require('system'),
    page_address;
var fs = require('fs');
if (system.args.length === 1){
  console.log('Usage: phantomjs ' + system.args[0] + ' <page_to_load:http://www.google.com>');
  phantom.exit();
}
page_address = system.args[1]

page.open(page_address, function(status){
    console.log('Status:' + status);
    if (status === 'success' ){
      fs.write('phantom_result.html', page.content, 'w')
    }
    phantom.exit();
});
var page=require('webpage')。create(),
系统=要求(“系统”),
第页地址;
var fs=需要('fs');
if(system.args.length==1){
log('用法:phantomjs'+system.args[0]+'');
phantom.exit();
}
page_address=system.args[1]
第页打开(第页地址、功能(状态){
console.log('状态:'+状态);
如果(状态==“成功”){
fs.write('phantom_result.html',page.content,'w')
}
phantom.exit();
});

您能在本地主机上提供该页面,然后用phantom将其删除吗?我认为@Tim正在尝试解析html文件并在标记中插入文本。BeautifulSoup应该完成这项工作。使用WebClient并从中获取结果dom如何?我想你可以这样做,但还没有测试过,因此是评论而不是答案。@Daniel_L该页面是由主机提供的,我没有control@DarrenGourley你是说这个吗?如果是这样的话,那么它会在我的页面处理过程中刹车。不幸的是,我会很快检查这个。您是否熟悉其他开源堆栈库(java、scala、clojure)?抱歉@Tim,根据我上面的评论,我认为这个问题带有.NET标记。我不知道还有其他图书馆会这样做。