怎么用python修改该html页面?

原有页面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结构嵌套更多,硬编码是无意义的。)

阅读 10.6k
5 个回答
import re
with open('1.html', 'r') as r:
    txt = ''.join(r.readlines())

print(txt)  # 你原始的html文本

def replace(match):
    t, s = match.group(1), match.group(1).strip()
    return '>%s<' % (t.replace(s, str(len(s))) if s else t)


txt1 = re.sub(r'>([.\S\s]*?)<', replace, txt)

print(txt1)  # 转换后的html
import re

def f(m):
    s = m.group(1)
    length = len(s.strip())
    if length == 0:
        return '>{}<'.format(s)
    return '>{}<'.format(re.sub('\S+.?\S?', str(length), s))

p = re.compile('>(.*?)<', re.S)
print(p.sub(f, html))

去找一个html解析器,转化后的结构找到text节点,替换成文本的长度

建议用javascript啊, 不能再简单了

浏览器F12, 粘贴下面的代码到console里.

function walk(node, fn) {
    if (node) do {
            fn(node);
            walk(node.firstChild, fn);       
    } while (node = node.nextSibling);
}
 
walk(document.body, function(node) {
        if(node.nodeType==1 || node.nodeType==3){       
                console.log(node.nodeValue);   
                node.nodeValue = (node.nodeValue+"").length;       
    }
});

有图有真相:

图片描述

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题