HTML Link Extractor Function

  • Share this:

Code introduction


This function retrieves HTML content from a specified URL and parses it using a specified parser (default is 'html.parser'). It extracts all hyperlinks (href attributes of 'a' tags) from the HTML content.


Technology Stack : beautifulsoup4, requests

Code Type : Function

Code Difficulty : Intermediate


                
                    
def extract_links(url, parser='html.parser'):
    from bs4 import BeautifulSoup
    import requests
    
    response = requests.get(url)
    soup = BeautifulSoup(response.text, parser)
    links = [link.get('href') for link in soup.find_all('a')]
    return links