Extract Headings from URL with BeautifulSoup

  • Share this:

Code introduction


This function fetches HTML content from a specified URL, parses it using BeautifulSoup, and returns the text of all headings with the specified tag.


Technology Stack : BeautifulSoup, requests

Code Type : Function

Code Difficulty : Intermediate


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

    # Fetch the content from the URL
    response = requests.get(url)
    # Parse the content with BeautifulSoup
    soup = BeautifulSoup(response.content, 'html.parser')
    # Find all elements with the specified tag
    headings = soup.find_all(tag)
    # Extract and return the text of all headings
    return [heading.get_text() for heading in headings]

# JSON representation of the code