dom保存html,javascript – 将DOM操作应用于HTML并保存结果?

虽然第一部分确实可以在“文本模式”中使用正则表达式或JavaScript中更完整的DOM实现来解决,但对于第二部分(高度计算),您需要一个真实的完整浏览器或像PhantomJS这样的无头引擎.

PhantomJS is a command-line tool that packs and embeds WebKit.

Literally it acts like any other WebKit-based web browser, except that

nothing gets displayed to the screen (thus, the term headless). In

addition to that, PhantomJS can be controlled or scripted using its

JavaScript API.

下面是一个示意性指令(我承认未经测试).

在您的修改脚本(例如,modify-html-file.js)中打开一个HTML页面,修改它的DOM树和console.log根元素的HTML:

var page = new WebPage();

page.open(encodeURI('file://' + phantom.args[0]), function (status) {

if (status === 'success') {

var html = page.evaluate(function () {

// your DOM manipulation here

return document.documentElement.outerHTML;

});

console.log(html);

}

phantom.exit();

});

接下来,通过将脚本的输出重定向到文件来保存新的HTML:

#!/bin/bash

mkdir modified

for i in *.html; do

phantomjs modify-html-file.js "$1" > modified/"$1"

done

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值