What are the 3 parts to a URL in Python?

Feb 20, 2024 ยท 2 min read

Understanding URLs (Uniform Resource Locators) is key for web development in Python. A URL points to a unique web resource like a web page, image, or file.

When working with URLs in Python, it's helpful to break them down into 3 main components:

The Protocol

The protocol indicates how the resource should be accessed. The most common protocols are:

  • http:// - Access the resource over HTTP
  • https:// - Access the resource over HTTPS (secure HTTP)
  • ftp:// - Access the resource over FTP
  • For example:

    url = "http://www.example.com/index.html"

    Here the protocol is http://

    The Domain Name

    The domain name points to the web server where the resource is located. For example:

    url = "http://www.example.com/index.html"

    Here the domain name is www.example.com.

    Domain names must be registered and mapped to an IP address.

    The Path

    The path shows the specific resource being requested on the server. For example:

    url = "http://www.example.com/products/shoes.jpg"

    Here the path is /products/shoes.jpg pointing to a specific image file.

    The path can have multiple parts separated by slashes. If no path is specified, the web server returns the default resource, usually index.html.

    So in Python if you need to handle URLs, breaking them into these 3 parts makes it easy to validate, parse and manipulate them. The urllib and urlparse modules provide handy functions for working with URLs.

    Browse by tags:

    Browse by language:

    The easiest way to do Web Scraping

    Get HTML from any page with a simple API call. We handle proxy rotation, browser identities, automatic retries, CAPTCHAs, JavaScript rendering, etc automatically for you


    Try ProxiesAPI for free

    curl "http://api.proxiesapi.com/?key=API_KEY&url=https://example.com"

    <!doctype html>
    <html>
    <head>
        <title>Example Domain</title>
        <meta charset="utf-8" />
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
    ...

    X

    Don't leave just yet!

    Enter your email below to claim your free API key: