You can download this code by clicking the button below.
This code is now available for download.
This function extracts the text of all elements with a specified tag name from the given HTML content.
Technology Stack : BeautifulSoup
Code Type : Function
Code Difficulty : Intermediate
def extract_titles_from_html(html_content, tag_name='h2'):
from bs4 import BeautifulSoup
# Create a BeautifulSoup object
soup = BeautifulSoup(html_content, 'html.parser')
# Find all elements with the specified tag name
elements = soup.find_all(tag_name)
# Extract and return the text of the elements
titles = [element.get_text() for element in elements]
return titles
# JSON representation of the code