Extract Links from HTML by Tag and Class

  • Share this:

Code introduction


This function extracts all links from a given HTML string using specified tags. If a class name is provided, it only extracts links from tags with that class.


Technology Stack : beautifulsoup4

Code Type : Function

Code Difficulty : Intermediate


                
                    
def extract_links_from_html(html_string, tag='a', class_name=None):
    from bs4 import BeautifulSoup
    from bs4.element import Tag

    soup = BeautifulSoup(html_string, 'html.parser')
    if class_name:
        links = soup.find_all(tag, class_=class_name)
    else:
        links = soup.find_all(tag)
    
    return [link.get('href') for link in links if link.get('href')]