Skip to content

Instantly share code, notes, and snippets.

Last active July 24, 2021 01:47
What would you like to do?
import urllib.request
import base64
import sys
import os
# BASIC認証を使用する: 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)):
with open(filename, mode="wb") as f:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment