Get Links from a Webpage with Python

Use Aspose.HTML for Python via .NET to extract links from HTML files or webpages. Load the document with HTMLDocument, find its <a> elements, and read their href attributes. Resolve relative references against the document’s base URI when your application needs complete URLs for further processing.

Reading a link retrieves its destination address; it does not visit the destination or download its content. The HTML DOM also lets you read the anchor text when a URL list needs labels.


Python Code to Extract Links from a Webpage

This example collects HTTP and HTTPS links from one webpage and resolves relative references to absolute URLs. It removes exact duplicate addresses and saves them in sorted order to extracted-links.txt in the current working directory. Each run replaces the output file.

Save unique webpage URLs to a text file

import aspose.html as ah

with ah.HTMLDocument("https://products.aspose.com/html/python-net/") as document:
    urls = set()
    for link in document.get_elements_by_tag_name("a"):
        href = link.get_attribute("href")
        if href:
            url = ah.Url(href, document.base_uri).href
            if url.startswith(("http://", "https://")):
                urls.add(url)

    with open("extracted-links.txt", "w", encoding="utf-8") as file:
        file.write("\n".join(sorted(urls)))



How to Extract Website Links in Python

  1. Load the webpage with HTMLDocument and collect its <a> elements with get_elements_by_tag_name("a").
  2. Read each link’s destination using get_attribute() .
  3. Use Url with document.base_uri to resolve relative references, then keep unique HTTP and HTTPS addresses.
  4. Write the URLs to a UTF-8 text file.



HTML Link Extraction Documentation

See Extract Links from HTML in Python for file input, anchor text, and link selection examples. The HTML Navigation guide covers selecting links from a particular content area with CSS selectors.



Install Aspose.HTML for Python via .NET

Install the aspose-html-net package from PyPI .

Install Aspose.HTML for Python via .NET

pip install aspose-html-net

Review the installation guide and system requirements before deployment. Request a temporary license to test the API without evaluation limitations.



HTML Link Extraction in Python FAQ

1. How do I extract internal and external links from a webpage in Python?

Collect the link URLs and compare each resolved URL’s hostname with the source page’s hostname. Links to the same host are internal under that rule; links to another host are external. Decide whether subdomains should count as the same website before applying the comparison.

2. How can I find links to PDF files on a webpage using Python?

Read the page’s link destinations and select URLs whose pathname ends with .pdf. Check the pathname rather than the complete URL so a query string does not hide the extension. This finds links to PDF files; retrieving the PDF content is a separate download step.

3. How do I get links from every page of a website in Python?

Process the required webpages and collect links from each one. Keep track of visited pages to avoid processing the same page repeatedly. Reading links from one HTML document does not automatically crawl the whole website, and the required links must be present in the loaded page content.