We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Sections of a URL

URLs have quite a few sections. Some are required, some are not.

Assignment

Let's use urlparse again to parse a URL and extract all the different parts. We'll learn more about each part later, for now, let's just split a URL and return its components!

Complete the parse_url function. It should return a dictionary containing all the parts of a URL. For example, given this URL:

http://testuser:[email protected]:8080/testpath?testsearch=testvalue#testhash

Your function should return:

{
    "protocol": "http",
    "username": "testuser",
    "password": "testpass",
    "hostname": "testdomain.com",
    "port": 8080,
    "pathname": "/testpath",
    "search": "testsearch=testvalue",
    "hash": "testhash",
}

You'll need to use the urlparse function and extract the following components:

  • protocol (scheme)
  • username
  • password
  • hostname
  • port
  • pathname (path)
  • search (query)
  • hash (fragment)

If any component is None, return an empty string "" for that component instead.