极客小将

您现在的位置是:首页 » python编程资讯

资讯内容

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

极客小将2021-02-27-
简介python清除html文件中内容的方法:1、使用join方法,代码为【pat=re.compile('>(.*?)<')''.join(pat.findall(test))】;2、使用compile方法。本教程操作环境:windows7系统、python

python清除html文件中内容的方法:1、使用join方法,代码为【pat = re.compile('>(.*?)<')''.join(pat.findall(test))】;2、使用compile方法。bzG少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

bzG少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

本教程操作环境:windows7系统、python3.9版,DELL G3电脑,该方法适用于所有品牌电脑。bzG少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

python清除html文件中内容的方法:bzG少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

方法1:bzG少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

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:bzG少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

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:bzG少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

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

方法4:bzG少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

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

大量免费学习推荐,敬请访问python教程(视频)bzG少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

以上就是python如何清除html文件中的内容的详细内容,更多请关注少儿编程网其它相关文章!bzG少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

网友点评

共有5条评论来说两句吧...

在线客服