from bs4.element import Tag
+def _changeTag(tag: Tag, new_name: str) -> Tag:
+ tag.name = new_name
+ return tag
+
+
DOMFilters: List[Tuple[str, Callable[[Tag], Any]]] = [
("NoEdit", lambda x: [eb.decompose() for eb in x.find_all("div", class_="post-edit-box")]),
("NoFooter", lambda x: [foot.decompose() for foot in x.find_all("div", class_="post-footer")]),
+ ("Strike", lambda x: [_changeTag(span, 's')
+ for span in x.find_all("span", style="text-decoration: line-through;")]),
]
This is glowfic
<div class="post-footer">This is the footer</div>
</div>''', 'html.parser')
+ self._strike = BeautifulSoup(b'''
+ <div class="post-content">
+ <p><span style="text-decoration: line-through;">Abandon hope and endure.</span></p>
+ <p>No. Win.</p>
+ </div>''', 'html.parser')
def testStripFooters(self) -> None:
ApplyDOMFilters("NoFooter", self._html)
self.assertEqual(list(self._html.stripped_strings),
["This is glowfic"])
+ def testStrike(self) -> None:
+ ApplyDOMFilters('Strike', self._strike)
+ s = self._strike.find_all('s')
+ self.assertEqual(len(s), 1)
+ self.assertEqual(list(s[0].stripped_strings),
+ ['Abandon hope and endure.'])
+
if __name__ == '__main__':
unittest.main()