Extract HTML Links by Tag and Class

  • Share this:

Code introduction


This function extracts all links from a given HTML content with specified tag and class.


Technology Stack : Beautiful Soup, HTML parsing

Code Type : Function

Code Difficulty : Intermediate


                
                    
def extract_links_from_html(html_content, tag='a', class_='link'):
    from bs4 import BeautifulSoup
    soup = BeautifulSoup(html_content, 'html.parser')
    links = soup.find_all(tag, class_=class_)
    return [link.get('href') for link in links]