[英]JavaScript to Html
給定一個包含JavaScript代碼的網頁,我想自動生成一個結果html(通過CLI工具或使用某種語言的某些庫)
例如,給出test.html
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hello JavaScript!";
</script>
</body>
</html>
我希望得到結果
<html>
<body>
<p id="demo">Hello JavaScript!</p>
<script>
document.getElementById("demo").innerHTML = "Hello JavaScript!";
</script>
</body>
</html>
經過快速搜索,看起來watin會做你想做的事。
它的目標是自動化測試,但當它點擊一個頁面時,它將執行所有js以及ajax調用等。看起來你也可以從中獲取生成的html。
答案是根據@torazaburo的評論
實際上, phantomjs能夠評估javascript並生成html。
以下是它的外觀,執行phantomjs load_page.js path_to/test.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();
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.