Finding HTTP Links in HTML

  • Share this:

Code introduction


This function finds all links that start with 'http' in the given HTML content. The default tag to look for is 'a', and the attribute name for the link is 'href'.


Technology Stack : beautifulsoup4

Code Type : Function

Code Difficulty : Intermediate


                
                    
def find_random_links(html_content, tag_name="a", attribute_name="href"):
    from bs4 import BeautifulSoup
    soup = BeautifulSoup(html_content, 'html.parser')
    links = soup.find_all(tag_name, attrs={attribute_name: lambda x: x and x.startswith("http")})
    return [link.get(attribute_name) for link in links]