Creation/Python
[Python] 다운로드 %를 보여주는 urllib2를 이용한 인터넷 파일 다운로드
Readiz
2013. 11. 6. 01:55
import urllib2
url = "http://download.thinkbroadband.com/10MB.zip"
file_name = url.split('/')[-1]
u = urllib2.urlopen(url)
f = open(file_name, 'wb')
meta = u.info()
file_size = int(meta.getheaders("Content-Length")[0])
print "Downloading: %s Bytes: %s" % (file_name, 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()
Source: http://stackoverflow.com/questions/22676/how-do-i-download-a-file-over-http-using-python
물론 간단히 네줄이면 끝난다. 파이썬 강점이 직관적인 프로그램 작성이 아니겠는가.
f = open('test.html', 'wb')
response = urllib2.urlopen("http://readiz.com/)
f.write(response.read())
f.close()
두가지 경우의 경우 모두 다 알고 있으면 더 좋을 듯 싶다.