Skip to content

Instantly share code, notes, and snippets.

@Paradoxis
Created September 24, 2018 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Paradoxis/89eb79bbed2279332c43f7d4f2e65111 to your computer and use it in GitHub Desktop.
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
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