如何在 Linux 终端上下载网页?
operating systemlinuxmca
Linux 命令行除了本身具备处理 Web 服务器和 Web 浏览的功能外,还提供了强大的 Web 爬虫功能。本文将介绍一些 Linux 环境中可用或可安装并用于离线 Web 浏览的工具。离线 Web 浏览的基本操作是下载一个或多个网页。
Wget
Wget 可能是所有下载工具中最著名的一个。它支持从 http、https 以及 FTP 服务器下载。它可以下载整个网站,也支持代理浏览。
以下是安装和使用 Wget 的步骤。
检查 Wget 是否可用
ubuntu@ubuntu:~$ which wget ; echo $?
运行上述代码得到以下结果:
/usr/bin/wget 0
如果退出代码($?)为1,则运行以下命令安装wget。
ubuntu@ubuntu:~$ sudo apt-get install wget
现在我们运行wget命令下载指定的网页或网站。
#下载网页 wget https://en.wikipedia.org/wiki/Linux_distribution # 下载整个网站 wget abc.com
运行上述代码得到以下结果。我们仅显示网页的结果,而不是整个网站的结果。下载的文件将保存在当前目录中。
ubuntu@ubuntu:~$ wget https://en.wikipedia.org/wiki/Linux_distribution --2019-12-29 23:31:41-- https://en.wikipedia.org/wiki/Linux_distribution Resolving en.wikipedia.org (en.wikipedia.org)... 103.102.166.224, 2001:df2:e500:ed1a::1 Connecting to en.wikipedia.org (en.wikipedia.org)|103.102.166.224|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 216878 (212K) [text/html] Saving to: ‘Linux_distribution’ Linux_distribution 100%[===================>] 211.79K 1.00MB/s in 0.2s 2019-12-29 23:31:42 (1.00 MB/s) - ‘Linux_distribution’ saved [216878/216878]
cURL
cURL 是一个客户端应用程序。它支持从 http、https、FTP、FTPS、Telnet、IMAP 等协议下载文件。与 wget 相比,它支持不同类型的下载。
以下是安装和使用的步骤。
检查 cURL 是否可用
ubuntu@ubuntu:~$ which cURL ; echo $?
运行上述代码得到以下结果:
1
值为 1 表示系统中 cURL 不可用。因此,我们将使用以下命令进行安装。
ubuntu@ubuntu:~$ sudo apt-get install curl
运行上述代码后,我们将得到以下结果,表明 cURL 已安装。
[sudo] password for ubuntu: Reading package lists... Done …. Get:1 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 curl amd64 7.47.0-1ubuntu2.14 [139 kB] Fetched 139 kB in 21s (6,518 B/s) ……. Setting up curl (7.47.0-1ubuntu2.14) ...
接下来我们使用 cURL 下载网页。
curl -O https://en.wikipedia.org/wiki/Linux_distribution
运行上述代码后,我们将得到以下结果。您可以在当前工作目录中找到下载的文件。
% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 211k 100 211k 0 0 312k 0 --:--:-- --:--:-- --:--:-- 311k
相关文章
有用资源
linux 参考教程 - 该教程包含有关 linux 的更多信息:https://www.cainiaomax.com/linux/

