Shakerato

Python code to download file on the internet using urllib library (python 3) 본문

Research

Python code to download file on the internet using urllib library (python 3)

Shakeratto 2018. 6. 1. 10:50
import urllib.request as urllib2
import time
import logging

def fileDownload(url, file_name):
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('[fileDownload]')

num_of_retries = 3
time_interval = 2

for retrycnt in range(num_of_retries):
try:
u = urllib2.urlopen(url)
break
except Exception as e:
logger.error(' Faild to download: ' + str(e) + ', Number of remaining attempts: ' \
+ str(num_of_retries - retrycnt - 1))
time.sleep(time_interval)

f = open(file_name, 'wb')
meta = u.info()
file_size = int(dict(meta.items())['Content-Length'])
print('Downloading: ', file_name, ' Bytes: ', file_size)

file_size_dl = 0
block_sz = 8192
while True:
buffer = u.read(block_sz)
if not buffer:
break

file_size_dl += len(buffer)
f.write(buffer)
status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
status = status + chr(8)*(len(status)+1)
print(status)

f.close()

if __name__ == "__main__":
url = '[file_url_to_download]'
file_name = '[file_name_to_save]'
fileDownload(url, file_name)


my github url:

https://github.com/entymos/crawling/blob/master/python3_file_download


refer to here:

https://stackoverflow.com/questions/22676/how-do-i-download-a-file-over-http-using-python?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa


https://codereview.stackexchange.com/questions/112868/retrying-a-web-request-in-python?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa



Comments