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