欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

python如何清除html文件中的内容

程序员文章站 2022-03-13 20:42:23
...

python清除html文件中内容的方法:1、使用join方法,代码为【pat = re.compile('>(.*?)<')''.join(pat.findall(test))】;2、使用compile方法。

python如何清除html文件中的内容

本教程操作环境:windows7系统、python3.9版,DELL G3电脑,该方法适用于所有品牌电脑。

python清除html文件中内容的方法:

方法1:

In [97]: str_ = ''
    ...: flag = 1
    ...: for ele in test:
    ...:     if ele == "<":
    ...:         flag = 0
    ...:     elif ele == '>':
    ...:         flag = 1
    ...:         continue
    ...:     if flag == 1:
    ...:         str_ += ele
    ...:         
In [98]: str_
Out[98]: 'just for testjust for testtest'
In [99]: str_ = ''
    ...: flag = 1
    ...: for ele in test:
    ...:     if ele == "<":
    ...:         flag = 0
    ...:     elif ele == '>':
    ...:         flag = 1
    ...:         ele = ' '
    ...:     if flag == 1:
    ...:         str_ += ele
    ...:         
In [100]: str_
Out[100]: ' just for test   just for test  test '

方法2:

import re
In [156]: pat = re.compile('(?<=\>).*?(?=\<)')
In [157]: pat.findall(test)
Out[157]: ['just for test', '', '', 'just for test', '', 'test']
In [158]: ''.join(pat.findall(test))
Out[158]: 'just for testjust for testtest'

方法3:

pat = re.compile('>(.*?)<')
''.join(pat.findall(test))

方法4:

In [167]: pat = re.compile('<[^>]+>', re.S)
In [168]: pat.sub('', test)
Out[168]: 'just for testjust for testtest'

大量免费学习推荐,敬请访问python教程(视频)

以上就是python如何清除html文件中的内容的详细内容,更多请关注其它相关文章!

相关标签: python html文件