Extract Hyperlinks from Webpage URL

  • Share this:

Code introduction


This function takes a webpage URL and a parser name as arguments, and returns a list of URLs of all hyperlinks found in the webpage.


Technology Stack : beautifulsoup4, requests

Code Type : Function

Code Difficulty : Intermediate


                
                    
def extract_links(url, parser='html.parser'):
    from bs4 import BeautifulSoup
    import requests

    # Fetch the webpage content
    response = requests.get(url)
    # Parse the content using BeautifulSoup
    soup = BeautifulSoup(response.content, parser)
    # Find all the links in the webpage
    links = soup.find_all('a', href=True)
    # Extract and return the href attribute from each link
    return [link['href'] for link in links]