Last active
August 29, 2015 13:56
-
-
Save tyochiai/8817420 to your computer and use it in GitHub Desktop.
第一引数の URL を第二引数のファイル名でカレントディレクトリに保存する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import urllib.request | |
def reporthook(blocknum, bs, size): | |
if size <= -1: | |
# サイズが取得できなかった場合は -1 が渡される | |
# 表示しようがないので何も表示しない | |
return | |
if size == 0: | |
# 0 除算避け | |
return | |
part_size = bs * blocknum | |
if part_size > size: | |
part_size = size | |
progress = part_size / size * 100 | |
msg = "\rprogress: {:.0f}% ({}/{})".format(progress, part_size, size) | |
print(msg, end="") | |
def main(): | |
if len(sys.argv) < 3: | |
print("usage: python3 downloader.py [URL] [output_name]") | |
sys.exit(1) | |
try: | |
urllib.request.urlretrieve(sys.argv[1], sys.argv[2], reporthook) | |
print("\n完了!") | |
except: | |
print("\nなんかエラー!") | |
finally: | |
urllib.request.urlcleanup() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment