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教程(视频)
以上是python如何清除html文件中的内容的详细内容。更多信息请关注PHP中文网其他相关文章!