原有页面html代码:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta content="pdf2htmlEX" name="generator" />
<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible" />
<title></title>
</head>
<body>
<div class="t m0 x0 h3 y45e ff2 fs1 fc1 sc0 ls0 ws102">
abcd
<span class="ws0">adc</span>
</div>
<div class="t m0 x0 h3 y45f ff2 fs1 fc1 sc0 ls0 wse">
ab
</div>
<div class="t m0 xd5 hb y4be ff2 fs3 fc1 sc0 ls7 wse3">
SUP
<span class="_ _93"> </span>
OUT
<span class="_ _a1"> </span>
OUT
</div>
<div class="t m0 xff h3 y4c1 ff2 fs1 fc1 sc0 ls5c ws10b">
(V
<span class="_ _54"> </span>
V
<span class="_ _a0">b<span class="_ _92">aa</span></span>
V
</div>
</body>
</html>
要用python程序,将该html页面修改为如下模样:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta content="pdf2htmlEX" name="generator" />
<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible" />
<title></title>
</head>
<body>
<div class="t m0 x0 h3 y45e ff2 fs1 fc1 sc0 ls0 ws102">
4
<span class="ws0">3</span>
</div>
<div class="t m0 x0 h3 y45f ff2 fs1 fc1 sc0 ls0 wse">
2
</div>
<div class="t m0 xd5 hb y4be ff2 fs3 fc1 sc0 ls7 wse3">
3
<span class="_ _93">1</span>
3
<span class="_ _a1">1</span>
3
</div>
<div class="t m0 xff h3 y4c1 ff2 fs1 fc1 sc0 ls5c ws10b">
2
<span class="_ _54">1</span>
2
<span class="_ _a0">1<span class="_ _92">2</span></span>
1
</div>
</body>
</html>
对比两个页面代码,可以看到,是要将每一个标签内的每一个text替换为该text的位数,同时要保证原有的dom结构与标签属性不发生任何改变,最后要将结果保存为新页面。
我用beautifulsoup怎么搞也搞不出来,是这个需求太怪异了吗?求大神帮助。(上面的页面只是示例,真实页面dom结构嵌套更多,硬编码是无意义的。)