Created
September 24, 2018 14:46
-
-
Save Paradoxis/89eb79bbed2279332c43f7d4f2e65111 to your computer and use it in GitHub Desktop.
Simple function that 'flattens' HTML documents by removing tags but replacing them with their original content, not too shabby on performance but it works
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def flatten_html(html, *remove_tags): | |
copy = html | |
for name in remove_tags: | |
while True: | |
soup = BeautifulSoup(copy, 'html.parser') | |
tag = soup.find(name) | |
if not tag: | |
break | |
parent = tag.parent | |
children = tag.contents | |
index = parent.contents.index(tag) | |
parent.contents[index:index] = children | |
parent.contents.remove(tag) | |
copy = soup.prettify() | |
return copy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment