Extracting Links from a URL Using BeautifulSoup

  • Share this:

Code introduction


This function is used to extract all links from a specified URL. It sends an HTTP request to the given URL, then parses the HTML content using BeautifulSoup, and extracts the href attributes from all found anchor tags.


Technology Stack : beautifulsoup4, requests

Code Type : Function

Code Difficulty : Intermediate


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

    # Send a request to the URL
    response = requests.get(url)
    # Parse the HTML content
    soup = BeautifulSoup(response.text, parser)
    # Find all anchor tags and extract the href attribute
    links = [a['href'] for a in soup.find_all('a', href=True)]
    return links