使用 Wget 與 curl 下載資料
在啟動一個資料分析專案時,先把所有資料集中到同一個位置是良好做法。這通常代表要從不同來源下載與擷取資料,例如 HTTP 伺服器與資料庫。
curl 很方便用來下載單一檔案,但在處理多檔案下載時就不太順手。在這個總結練習中,你會同時使用 curl 與 Wget 下載一系列每月的 Spotify 檔案,進行一些簡單處理,並把所有下載的檔案統一集中到本機目錄。
本練習屬於課程
在 Shell 中進行資料處理
練習說明
- 使用
curl從經過縮短(重新導向)的 URL 下載壓縮檔201812SpotifyData,並在同一步驟中將檔名改為Spotify201812.zip。 - 解壓縮
Spotify201812.zip、刪除原始壓縮檔,並將解壓後的檔案重新命名為Spotify201812.csv以保持一致性。 - 使用
url_list.txt搭配Wget,在同一步驟中下載 3 個檔案:Spotify201809.csv、Spotify201810.csv、Spotify201811.csv,並將下載速度上限設定為 2500KB/s。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Use curl, download and rename a single file from URL
___ ___ Spotify201812.zip ___ https://assets.datacamp.com/production/repositories/4180/datasets/eb1d6a36fa3039e4e00064797e1a1600d267b135/201812SpotifyData.zip
# Unzip, delete, then re-name to Spotify201812.csv
unzip Spotify201812.zip && rm Spotify201812.zip
mv 201812SpotifyData.csv ___.csv
# View url_list.txt to verify content
cat url_list.txt
# Use Wget, limit the download rate to 2500 KB/s, download all files in url_list.txt
wget ___=2500k -i url_list.txt
# Take a look at all files downloaded
ls