【Python】PythonでWeb上のファイルを操作(ダウンロード)
1.URLからHTMLや画像や音楽etc...をダウンロードする
urlretrieve()関数を使うことでインターネット上のサイトや画像をダウンロードすることができます。
urlretrieve関数の第一引数にURL、第二引数に保存する名前(拡張子も)を代入します。
Webサイトの場合
#-*- coding:utf-8 -*- import urllib.request URL = "https://shizenkarasuzon.hatenablog.com/about" Name = "sample.html" urllib.request.urlretrieve(URL, Name)
画像の場合
#-*- coding:utf-8 -*- import urllib.request URL = "https://cdn-ak.f.st-hatena.com/images/fotolife/p/pythonjacascript/20181126/20181126232555.jpg" Name = "C:/Users/Owner/Pictures/sample.jpg" urllib.request.urlretrieve(URL, Name)
このように保存する場所を指定することもできます。
URL = "https://cdn-ak.f.st-hatena.com/images/fotolife/p/pythonjacascript/20181126/20181126232555.jpg" Name = "C:/Users/Owner/Pictures/sample.jpg" urllib.request.urlretrieve(URL, Name)
WebサイトのHTMLを文字データで取得
#-*- coding:utf-8 -*- import urllib.request html = urllib.request.urlopen('https://shizenkarasuzon.hatenablog.com/about').read() print(html)
サイトの情報を取得
#-*- coding:utf-8 -*- import urllib.request URL = "http://python.org/" conn = urllib.request.urlopen(URL) Info = conn.info() print(Info["Date"]) print(Info["Server"]) print(Info["Content-Length"]) print(Info["Age"]) print(Info["Connection"]) print(Info["Vary"])
上のようなプログラムを実行すると、下のように出力されます。(2019/08/16)
Fri, 16 Aug 2019 11:29:54 GMT nginx 48535 2704 close Cookie
info()関数を使うことで、指定したURLの情報を指定することができます。
info()関数の戻り値は辞書オブジェクトです。
urlretrieve関数の戻り値を使って情報を取得することもできます。
#-*- coding:utf-8 -*- import urllib.request URL = "http://python.org/" fn, h = urllib.request.urlretrieve(URL) print(h.items())
結果:
[('Server', 'nginx'), ('Content-Type', 'text/html; charset=utf-8'), (略) ('Via', '1.1 varnish'), ('Age', '3489'), ('Connection', 'close')]