1
0

More than 1 year has passed since last update.

【Python】Pythonでネット上にあるファイルをダウンロードする方法

Last updated at Posted at 2022-09-12

背景

  • 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でやった場合

  • コードだけだと、DLしたファイルの様子がわからないので、jupyterlabのスクショを添付しておく。
    file_dl_01.png

おまけ 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

file_dl_02.png

jupyterlabを黒くする方法

  • この記事を参照ください。

参考資料

  • Fisher, R.A.. (1988). Iris. UCI Machine Learning Repository.

個人ブログ

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0