A URL is the full address of something on the web. It is made of parts, and each part answers its own question: the scheme (how to talk to the server), the host (which computer), the port (which server on that computer), the path (what you want) and the query string (with which options, after the ?).
The function urlparse, from urllib.parse, splits a URL into those parts. Here it is on another URL:
site = urlparse("https://algocademy.com/blog?page=2")
print(site.hostname) # algocademy.com
print(site.port) # None: no port is written
print(site.query) # page=2
Scheme, host and port tell your script where to send the request. The path and the query travel inside the request and tell the server what you want. When a URL writes no port, the client uses 80 for http and 443 for https.
The starting code builds url for the supplier's cheap products and prints it. Your finished script must print:
URL: http://127.0.0.1:8001/prices?max_price=5 Scheme: http Host: 127.0.0.1 Port: 8001 Path: /prices Query: max_price=5
Your task: split url with urlparse and keep the result in parts, then print its scheme, host, port, path and query, each after its label.
Assignment
Follow the Coding Tutorial to split the supplier's URL into its five parts and print each one.
Hint
Each part is an attribute of the result: .scheme, .hostname, .port, .path and .query. The host is in .hostname.
Free plan: 30 tutor messages. PRO is unlimited.
