從網路匯入平面檔案

Python 資料匯入進階

Hugo Bowne-Anderson

Data Scientist at DataCamp

你已經很會匯入資料!

  • 平面檔,如 .txt 與 .csv

  • Pickle 檔、Excel 試算表等!

  • 關聯式資料庫中的資料

  • 以上都能在本機完成

  • 那若資料在網路上呢?

Python 資料匯入進階

你能匯入網路資料嗎?

第 1 章圖示 ch_1_1.010.png

  • 你可以:開啟網址並點擊下載檔案
  • 但:無法重現,難以擴充
Python 資料匯入進階

你將學到如何…

  • 從網路匯入並本機儲存資料集

  • 載入為 pandas 的 DataFrame

  • 發送 HTTP 請求(GET)

  • 擷取網頁資料,如 HTML

  • 將 HTML 解析為可用資料(BeautifulSoup)

  • 使用 urllib 與 requests 套件

Python 資料匯入進階

urllib 套件

  • 提供跨網路抓取資料的介面
  • urlopen():接受 URL 而非檔名
Python 資料匯入進階

如何用 Python 自動下載檔案

from urllib.request import urlretrieve
url = 'http://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/
winequality-white.csv'
urlretrieve(url, 'winequality-white.csv')
('winequality-white.csv', <http.client.HTTPMessage at 0x103cf1128>)
Python 資料匯入進階

一起來練習吧!

Python 資料匯入進階

Preparing Video For Download...