Keywords: Wget | File Download | Selective Filtering | Command Line Tool | Website Mirroring
Abstract: This article provides a comprehensive exploration of using the wget command-line tool to selectively download all files from a website except HTML, PHP, ASP, and other web page files. Based on high-scoring Stack Overflow answers, it systematically analyzes key wget parameters including -A, -m, -p, -E, -k, -K, and -np, demonstrating their combined usage through practical code examples. The guide shows how to precisely filter file types while maintaining website structure integrity, and addresses common challenges in real-world download scenarios with insights from reference materials.
Wget Tool Overview and Selective Download Requirements
Wget is a widely used command-line download tool in Linux and Unix systems, supporting HTTP, HTTPS, and FTP protocols with powerful features like recursive downloading and resumable transfers. In practical web data collection and website backup scenarios, users often need to download specific file types rather than entire website content. For instance, researchers might only require PDF documents and image files without needing HTML web pages.
Core Parameter Analysis and File Filtering Mechanism
Wget implements file type filtering through the --accept (abbreviated -A) parameter, which accepts a comma-separated list of file extensions. When combined with mirror mode, Wget downloads all files matching the specified extensions and automatically removes non-matching files.
The basic command structure is as follows:
wget -A pdf,jpg -m -p -E -k -K -np http://site/path/
The equivalent long option format is:
wget --accept pdf,jpg --mirror --page-requisites --adjust-extension --convert-links --backup-converted --no-parent http://site/path/
In-depth Parameter Function Analysis
--accept pdf,jpg: Specifies downloading only PDF and JPG format files, ignoring other file types. Wget matches files based on URL extensions, a mechanism that is simple and effective but relies on correct file extension naming.
--mirror: Enables mirror mode, equivalent to the combination of -r -N -l inf, implementing recursive downloading, timestamp checking, and unlimited depth traversal.
--page-requisites: Downloads all resources required to display complete web pages, including images, CSS, and JavaScript files. In selective download scenarios, this parameter ensures dependencies of required files are correctly downloaded.
--adjust-extension: Automatically adjusts file extensions based on server-returned MIME types, preventing file type identification errors due to improper server configuration.
Link Processing and Directory Structure Maintenance
--convert-links: Converts absolute links in downloaded files to relative links, enabling proper local browsing of downloaded website content. This feature is crucial for users needing to view downloaded content offline.
--backup-converted: Backs up original files before link conversion, preserving file integrity for subsequent comparison or recovery operations.
--no-parent: Restricts Wget from traversing upward to parent directories, ensuring download scope remains strictly within the specified path and preventing accidental download of unrelated content.
Practical Application Scenarios and Problem Solving
The directory traversal issue mentioned in reference materials reveals an important limitation of Wget in practical use: Wget can only download files accessible through links. If files are not referenced by any web page links or directory indexing is disabled, Wget cannot discover these files.
For such situations, consider the following solution:
wget --no-clobber --convert-links --random-wait -r -p -E -e robots=off -U mozilla http://site/path/
This command adds the --random-wait parameter to introduce random delays between requests, -e robots=off to ignore robots.txt restrictions, and -U mozilla to simulate browser user agents, improving download success rates in complex website structures.
Advanced Configuration and Best Practices
For scenarios requiring multiple file types, extend the accept parameter list:
wget -A pdf,jpg,png,doc,docx,xls,xlsx -m -p -E -k -K -np http://site/path/
During download processes, it's recommended to use the --limit-rate parameter to limit download speed and avoid excessive pressure on target servers:
wget -A pdf,jpg -m -p -E -k -K -np --limit-rate=100k http://site/path/
For websites requiring authentication, add username and password parameters:
wget -A pdf,jpg -m -p -E -k -K -np --user=username --password=password http://site/path/
Error Handling and Logging
Wget provides extensive logging and error handling options. The --output-file parameter redirects output to files for subsequent analysis:
wget -A pdf,jpg -m -p -E -k -K -np -o download.log http://site/path/
When encountering network connection issues, the --tries and --timeout parameters control retry attempts and timeout durations:
wget -A pdf,jpg -m -p -E -k -K -np --tries=5 --timeout=30 http://site/path/
Performance Optimization and Resource Management
In large-scale download tasks, proper configuration of concurrent connections and wait times is crucial:
wget -A pdf,jpg -m -p -E -k -K -np --wait=2 --random-wait --level=5 http://site/path/
This configuration waits 2 seconds between requests with random delays while limiting crawl depth to 5 levels, balancing download efficiency with responsible server request practices.
Conclusion and Future Perspectives
Wget, as a powerful command-line download tool, enables precise file type filtering and website structure maintenance through appropriate parameter combinations. The technical methods introduced in this article apply not only to simple file download tasks but also extend to complex data collection and website backup scenarios. As web technologies evolve, Wget continues to play a significant role in automation scripts and data processing pipelines.