The urllib module in Python provides useful tools for fetching data from the web. With just a few lines of code, you can make HTTP requests and parse responses without needing to use an external library.
Making GET Requests
To make a basic GET request, you first need to import urllib and call
import urllib.request
with urllib.request.urlopen('https://example.com') as response:
html = response.read()
The
Handling HTTP Responses
The response object has various useful methods and attributes for checking the status and handling the data:
print(response.status) # prints the status code e.g. 200
print(response.headers) # print the response headers
html = response.read() # read the response body
So with very little code, you can print out status codes, headers, and fetch the HTML content.
Constructing Requests
For more advanced usage, you can construct a Request object and customize details like headers and method:
request = urllib.request.Request('https://example.com',
headers={'User-Agent': 'My Program'})
response = urllib.request.urlopen(request)
Here we added a custom user-agent header to identify our program. This gives you full flexibility to tweak requests before sending.
Overall, urllib strikes a great balance between simplicity and control. Give it a try for your next Python project that needs to talk to web APIs or scrape websites!