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中文網其他相關文章!