from bs4 import BeautifulSoup import re def create_table_of_contents(html_content): """ Create a table of contents for a given HTML content. Args: html_content (str): HTML content of the blog post. Returns: str: HTML content with a table of contents. """ # Use BeautifulSoup to parse the HTML soup = BeautifulSoup(html_content, 'html.parser') # Find all header tags (h1, h2, h3, h4, h5, h6) headers = soup.find_all(re.compile('^h[1-6]$')) # Create a table of contents toc = BeautifulSoup('
Some text
Text under subtitle 1
Text under subtitle 2
" html_with_toc = create_table_of_contents(html_content) print(html_with_toc)