URLs (Uniform Resource Locators) are everywhere on the web. As a Python developer, you'll likely need to work with URLs to scrape web pages, interact with APIs, download files, and more. Here's a practical introduction to using URLs in Python.
Parsing URLs
Python has a built-in
For example, we can break a URL down into its individual components:
from urllib.parse import urlparse
url = 'https://www.example.com/path/to/file.html?key1=val1&key2=val2#SomewhereInTheDocument'
parsed_url = urlparse(url)
print(parsed_url)
This prints out a
ParseResult(scheme='https', netloc='www.example.com', path='/path/to/file.html', params='', query='key1=val1&key2=val2', fragment='SomewhereInTheDocument')
We can access the individual attributes like
Downloading Files from URLs
A common task is to download a file from a URL. We can use the
import urllib.request
url = 'https://www.example.com/files/report.pdf'
urllib.request.urlretrieve(url, 'report.pdf')
This downloads the file from the URL and saves it locally as
We can also customize the filename, display a progress bar, handle redirects, catch errors, and much more.
Interacting with APIs
Many web APIs accept URL parameters to filter data and return JSON/XML responses. We can use the
import requests
url = 'https://api.example.com/data?date=20200101&limit=50'
response = requests.get(url)
print(response.json())
The
In summary, Python has great URL handling capabilities out of the box. Whether you need to parse URLs, download files, call web APIs, or interact with websites, Python has you covered!