Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

データの取得

画像やデータを URL から取得して使うことがある.Python 標準ライブラリの urllib でダウンロードできる.

URL からファイルをダウンロード

urllib.request.urlretrieve(url, filename) は,URL の内容を取得してローカルの filename に保存する.

import urllib.request

url = "https://assets.ku-compbio.noshita.net/2026/P2-04/manifest.csv"
urllib.request.urlretrieve(url, "manifest.csv")
('manifest.csv', <http.client.HTTPMessage at 0x7f494c538450>)

保存先はカレントディレクトリ(作業フォルダ)である.同じコードは Colab でも動くが,保存先は仮想マシン上の作業領域(/content/)で,Google ドライブには残らず,セッション終了時に消える.

取得したファイルを読む

ダウンロード後は通常のローカルファイルとして扱える.表データ(CSV)は pandas,画像は matplotlib の plt.imread で読み込める.

import pandas as pd

df = pd.read_csv("manifest.csv")
df.head()
Loading...

画像を URL から取得して開く例は P2-04 を参照.

補足