如何在 Linux 終端下載網頁?
除了其固有的處理 Web 伺服器和 Web 瀏覽的能力外,Linux 命令列還提供強大的 Web 爬取功能。在本文中,我們將檢查一些在 Linux 環境中可用或可以安裝和使用的工具,用於離線 Web 瀏覽。這基本上是透過下載網頁或多個網頁來實現的。
Wget
Wget 可能是所有下載選項中最著名的一個。它允許從 http、https 以及 FTP 伺服器下載。它可以下載整個網站,並允許代理瀏覽。
以下是安裝和開始使用它的步驟。
檢查 wget 是否已可用
ubuntu@ubuntu:~$ which wget ; echo $?
執行以上程式碼,得到以下結果
/usr/bin/wget 0
如果退出程式碼($?)為 1,則執行以下命令安裝 wget。
ubuntu@ubuntu:~$ sudo apt-get install wget
現在,我們為要下載的特定網頁或網站執行 wget 命令。
#Downlaod a webpage wget https://en.wikipedia.org/wiki/Linux_distribution # Download entire website 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
廣告