Skip to content

1 min read中文

Mirroring a Web Page with wget

How to mirror a static web page with a single wget command, walking through what the --mirror, --convert-links, --adjust-extension, --page-requisites, and --no-parent flags each do.

Machine-translated from the Chinese original.

wget

Sometimes we need to save a web page, whether to browse offline or to archive / back it up.

For a dynamic page, you may need to dig into where its data comes from; but for a simple static page, a single wget command will do the job.

wget --mirror --convert-links --adjust-extension --page-requisites --no-parent https://example.com/something.html

What each flag does

--mirror

Downloads recursively.

--convert-links

Rewrites links (including links inside css). wget downloads all of a page’s resources, images, js, css, fonts, and so on, to local disk. --convert-links rewrites the original absolute references into relative ones, so the page is less likely to break when you browse it locally.

--adjust-extension

Fixes the extensions of html and css files based on the Content-Type header (note: it does not fix js extensions). For example, say a page originally references a css file named FunUI.css?15552165. Regardless of whether such a filename is even valid under NTFS naming rules, when loaded locally the browser won’t treat it as a stylesheet, since its extension isn’t .css. With --adjust-extension added, wget renames the file to [email protected], so the browser can recognize it correctly.

--page-requisites

Downloads css, images, and other content needed for the page to display correctly.

--no-parent

With this flag, wget won’t ascend to parent directories while recursing. Since we only want to save the current page, this flag is needed.

The command above can also be written as

wget -mkEpnp https://example.com/something.html

where np is short for --no-parent.

Did you know

wget has a single-file build for Windows, which is great for carrying around on a USB drive; here it is too: wget.exe.

See also