wget
The `wget` Python library (version 3.2) is a pure Python download utility designed for simple file retrieval from HTTP URLs. It provides basic functionality to download files, optionally displaying a progress bar. It is distinct from the widely-used command-line utility GNU Wget, which offers more advanced features. The package's last update was in 2015, making it largely unmaintained compared to newer alternatives.
Warnings
- breaking The `wget` library (version 3.2) has not been updated since October 2015 and is largely unmaintained. Consider using `py3-wget` for a more actively developed pure-Python solution or invoking the system's `wget` utility via Python's `subprocess` module for robust, feature-rich downloads.
- gotcha Installing the Python `wget` package via `pip install wget` does NOT install the command-line utility `wget` or provide a wrapper to it. It's a separate, pure-Python implementation. If you intend to use the powerful GNU Wget command-line tool, you must install it separately on your operating system (e.g., `apt install wget` on Linux, `brew install wget` on macOS) and then use Python's `subprocess` module to call it.
- gotcha The `wget.download()` function returns the local filename of the downloaded file. It does not return the file content or a simple boolean indicating success. If the file already exists, it will rename the new download (e.g., `file.txt.1`).
- gotcha When constructing URLs from user input or file reads (e.g., iterating lines from a text file), remember to strip any trailing newline characters (`\n`). If not removed, the URL passed to `wget.download()` will be invalid, often leading to download failures like HTTP 404 errors.
- gotcha This pure Python `wget` library has fewer features than the GNU Wget command-line utility. It lacks advanced capabilities like recursive downloads, complex authentication, `robots.txt` adherence, and robust resume functionality for interrupted downloads (though `py3-wget` and the CLI `wget` support these).
Install
-
pip install wget
Imports
- wget
import wget
Quickstart
import wget
import os
url = 'https://www.python.org/static/img/python-logo.png'
output_filename = 'python-logo.png'
# Ensure clean state for demonstration
if os.path.exists(output_filename):
os.remove(output_filename)
print(f"Downloading {url}...")
filename = wget.download(url, out=output_filename)
print(f"\nDownloaded to: {filename}")