Extract HTML Headings

  • Share this:

Code introduction


This function extracts text from all heading tags (h1-h6) within the given HTML content.


Technology Stack : Beautiful Soup

Code Type : Function

Code Difficulty : Intermediate


                
                    
def extract_headings(html_content):
    """
    Extracts headings from the given HTML content using BeautifulSoup.
    """
    from bs4 import BeautifulSoup
    soup = BeautifulSoup(html_content, 'html.parser')
    headings = soup.find_all(['h1', 'h2', 'h3', 'h4', 'h5', 'h6'])
    return [heading.get_text() for heading in headings]                
              
Tags: