背景
- Pythonでネット上にあるファイルをDLしてカレントに保存するコードが必要になった。
- 調べて一番簡単だと思った方法をまとめておく。
目標
- Pythonでネット上にあるファイルをDLできるようになる。
コード
- Pythonでネット上にあるファイルをDLする方法をまとめた以下のQuita記事さんを参考にした。
- この方が紹介されている一番簡単だと思ったコードを引用している。
https://qiita.com/toshikawa/items/2d535906b42b9332b743
pythonでネット上のファイルをDLするコード
import urllib.request
#DLしたいファイルがあるurlを指定
url= "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
#カレントディレクトリに保存したいファイル名を指定
save_name='python_iris.data'
#urllib.request.urlretrieveを使用してDL
urllib.request.urlretrieve(url, save_name)
※DLしているファイルはUCIのirisデータ
Fisher, R.A.. (1988). Iris. UCI Machine Learning Repository.
https://archive-beta.ics.uci.edu/ml/datasets/iris#Descriptive
jupyetlabでやった場合
おまけ curlコマンドでDLする場合
- 言わずもがなcurlコマンドでもファイルはDLできる
pythonでネット上のファイルをDLするコード
#remoteのファイル名(iris.data)でカレントにデータを保存
!curl -O https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data
#ファイル名(curl_iris.data)を指定してカレントにデータ保存
!curl -o curl_iris.data https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data
jupyterlabを黒くする方法
- この記事を参照ください。
参考資料
- Fisher, R.A.. (1988). Iris. UCI Machine Learning Repository.
個人ブログ