開始使用免費開始

使用 Wget 與 curl 下載資料

在啟動一個資料分析專案時,先把所有資料集中到同一個位置是良好做法。這通常代表要從不同來源下載與擷取資料,例如 HTTP 伺服器與資料庫。

curl 很方便用來下載單一檔案,但在處理多檔案下載時就不太順手。在這個總結練習中,你會同時使用 curlWget 下載一系列每月的 Spotify 檔案,進行一些簡單處理,並把所有下載的檔案統一集中到本機目錄。

本練習屬於課程

在 Shell 中進行資料處理

檢視課程

練習說明

  • 使用 curl 從經過縮短(重新導向)的 URL 下載壓縮檔 201812SpotifyData,並在同一步驟中將檔名改為 Spotify201812.zip
  • 解壓縮 Spotify201812.zip、刪除原始壓縮檔,並將解壓後的檔案重新命名為 Spotify201812.csv 以保持一致性。
  • 使用 url_list.txt 搭配 Wget,在同一步驟中下載 3 個檔案:Spotify201809.csvSpotify201810.csvSpotify201811.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
編輯並執行程式碼