You can download this code by clicking the button below.
This code is now available for download.
This function extracts all links from the given HTML content based on the specified tag and attribute.
Technology Stack : Beautiful Soup
Code Type : The type of code
Code Difficulty : Intermediate
def extract_links_from_html(html_content, tag='a', attribute='href'):
"""
Extracts all links from a given HTML content based on the specified tag and attribute.
Parameters:
html_content (str): The HTML content to parse.
tag (str): The tag to search for links. Default is 'a'.
attribute (str): The attribute of the tag to extract the link from. Default is 'href'.
Returns:
list: A list of extracted links.
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_content, 'html.parser')
links = [link.get(attribute) for link in soup.find_all(tag) if link.get(attribute)]
return links