DEV Community

MrRobot
MrRobot

Posted on

urllib – Python Standard Library for URL Handling

Title: urllib – Python Standard Library for URL Handling

Description:
urllib is a built-in Python library that provides modules for working with URLs. It supports opening, reading, and parsing URLs, handling HTTP requests, encoding and decoding query strings, and working with internet resources. Since it’s part of the standard library, it’s commonly used for web scraping, downloading files, or interacting with web APIs without requiring third-party libraries.


Installation:
(No installation required — comes with Python.)


Example usage:

from urllib import request, parse

# Fetch content from a URL
response = request.urlopen("https://www.example.com")
html = response.read().decode('utf-8')
print(html)

# Encode query parameters
params = {'q': 'python urllib'}
query_string = parse.urlencode(params)
url = f"https://www.google.com/search?{query_string}"
print(url)
Enter fullscreen mode Exit fullscreen mode

PyPI page: standard library — no separate PyPI page
GitHub page: https://github.com/python/cpython


3 Project Ideas:

  1. Build a simple web scraper to extract information from websites.
  2. Download images or files from a list of URLs.
  3. Create a small URL shortener or query string builder.

Top comments (0)