Skip to content

Instantly share code, notes, and snippets.

@t-asa2000
Last active July 24, 2021 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t-asa2000/629599b36ff6c39ead5d1a3573ffca28 to your computer and use it in GitHub Desktop.
Save t-asa2000/629599b36ff6c39ead5d1a3573ffca28 to your computer and use it in GitHub Desktop.
複数URLのファイルをまとめてダウンロードするPythonスクリプト
import urllib.request
import base64
import sys
import os
# BASIC認証を使用する: download.py usr psw
def auth(url):
usr = sys.argv[1]
psw = sys.argv[2]
basic_header = base64.b64encode('{}:{}'.format(usr,psw).encode('utf-8'))
return urllib.request.Request(url,headers={"Authorization": "Basic " + basic_header.decode('utf-8')})
# URLリスト(1行1ファイル)の読み込み
listfile = "download.txt"
with open(listfile, "r", encoding="utf-8") as f:
urls = f.readlines()
# ダウンロード
for url in urls:
eurl = urllib.parse.quote(url.rstrip(), safe='/:')
path = urllib.parse.urlparse(eurl).path
filename = urllib.parse.unquote(path).lstrip('/')
print("--ダウンロード中--\n" + eurl + "\n" + filename + "\n\n")
if(len(sys.argv) == 3): eurl = auth(eurl)
data = urllib.request.urlopen(eurl).read()
if not os.path.exists(os.path.dirname(filename)):
os.makedirs(os.path.dirname(filename))
with open(filename, mode="wb") as f:
f.write(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment