python 提取 html中的文字(用於rech text計算文字個數)


https://exceptionshub.com/python-code-to-remove-html-tags-from-a-string-duplicate.html

https://stackoverflow.com/questions/9662346/python-code-to-remove-html-tags-from-a-string

https://tutorialedge.net/python/removing-html-from-string/

https://stackoverflow.com/questions/753052/strip-html-from-strings-in-python  (最好)

 

以下,只使用python標准庫

from io import StringIO
from html.parser import HTMLParser

class MLStripper(HTMLParser):
    def __init__(self):
        super().__init__()
        self.reset()
        self.strict = False
        self.convert_charrefs= True
        self.text = StringIO()
    def handle_data(self, d):
        self.text.write(d)
    def get_data(self):
        return self.text.getvalue()

def strip_tags(html):
    s = MLStripper()
    s.feed(html)
    return s.get_data()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM