Coding/Python Matlab

파이썬 - 웹 링크 다운로드 (버퍼)

smores 2012. 3. 13. 03:12
import urllib2

def ReportError(msg, screenprinttoo = True):
    if screenprinttoo: print msg
    f = open("Error.log", "a")
    f.write(time.ctime() + " : " + msg + "\n")
    f.close()

def WebLinkSave(url, savedfilename):
    try:
        f = urllib2.urlopen(url)
    except:
        ReportError("Cannot save url: " + url)
        return None
    output = open(savedfilename,'wb',1024*1024*10)
    while True:
        binaryfile = f.read()
        if len(binaryfile)==0:
            break
        output.write(binaryfile)
    output.close()     
    return binaryfile


s=raw_input('url, filename(to save) : ')
s1=s.split()
url,savename=s1[0],s1[1]
WebLinkSave(url, savename)