HTML-filtering in Python: verschil tussen versies
Naar navigatie springen
Naar zoeken springen
Regel 33: | Regel 33: | ||
print(s) | print(s) | ||
print(o) | print(o) | ||
− | <pre> | + | </pre> |
Output: | Output: |
Versie van 13 jan 2019 12:36
Eerste voorbeeld
Gebaseerd op [1]:
#! /usr/bin/python3 # # parser-01.py - Jan. 2019 ############################################################################## # from html.parser import HTMLParser class MLStripper(HTMLParser): def __init__(self): self.reset() self.strict = False self.convert_charrefs= True self.fed = [] def handle_data(self, d): self.fed.append(d) def get_data(self): return ''.join(self.fed) def strip_tags(html): s = MLStripper() s.feed(html) return s.get_data() ############################################################################## # s="<h1> Hello, <strong>world</strong></h1>" o=strip_tags(s) print(s) print(o)
Output:
<h1> Hello, <strong>world</strong></h1> Hello, world
Zie ook
Bronnen
- https://stackoverflow.com/questions/753052/strip-html-from-strings-in-python - Uitgangspunt
- https://stackoverflow.com/questions/753052/strip-html-from-strings-in-python#13703994 - Preserve HTML-entiteiten