How to Convert wget Links for Offline Viewing?
Downloading a website for local browsing requires updating the
internal links so they point to local files instead of the internet. The
wget command-line utility provides a built-in feature to
handle this automatically during or after the download process. By using
the correct combination of flags, you can convert absolute and relative
URLs into working local paths, ensuring a seamless offline reading
experience.
The Essential Command Flags
To successfully download a webpage or an entire site and convert its
links for offline use, you rely on a few specific wget
options.
--convert-links(or-k): This is the core flag. It instructswgetto modify the links in the downloaded HTML documents after the download completes. It converts URLs to point to the locally downloaded files. If a link points to a file that wasn’t downloaded, it converts it to an absolute internet URL so it still works if you happen to regain connectivity.--page-requisites(or-p): This tellswgetto download all the elements necessary to display the HTML page correctly, such as images, stylesheets (CSS), and inline scripts.--recursive(or-r): If you want to download more than just a single page, this turns on recursive downloading, allowing you to follow links within the site up to a specified depth.
Practical Examples
Downloading a Single Page for Offline Viewing
If you only need a single article or webpage with all its styles and images intact, use the following command:
wget -p -k https://example.com/page.htmlIn this command, -p grabs the images and CSS, while
-k ensures that the HTML file updates its paths to find
those downloaded images and stylesheets on your local drive.
Mirroring an Entire Website
To clone an entire website for complete offline navigation, the
--mirror flag is often combined with link conversion:
wget --mirror --convert-links --page-requisites https://example.com/Note: The
--mirrorflag is a shortcut for infinite recursion, time-stamping, and keeping directory listings. When paired with--convert-links, it creates a fully browsable local replica of the target site.
How the Link Conversion Works
The link conversion process happens at the very end
of the download session. While wget is actively downloading
files, it keeps track of what it has saved and where. Once the entire
download queue is finished, it revisits the downloaded HTML files and
rewrites the URLs.
For instance, an image tag originally pointing to an online source:
<img src="https://example.com/images/logo.png">Will be rewritten to point to your local directory structure:
<img src="images/logo.png">This behavior ensures that you can open the root HTML file in any browser without an active internet connection, and the page will render exactly as it did online.