微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

本教程操作环境: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教程(视频)

原文地址:https://www.jb51.cc/python/1194646.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐