Extract Webpage Headings by Tag

  • Share this:

Code introduction


This function takes a URL and a tag type as arguments, retrieves the webpage content from the URL, parses the HTML using BeautifulSoup, and returns the text content of all specified tags.


Technology Stack : BeautifulSoup, requests

Code Type : Function

Code Difficulty : Intermediate


                
                    
def extract_headings(url, tag="h1"):
    from bs4 import BeautifulSoup
    import requests

    response = requests.get(url)
    soup = BeautifulSoup(response.text, 'html.parser')
    headings = soup.find_all(tag)
    return [heading.get_text() for heading in headings]

# JSON Explanation