How to know if urllib.urlretrieve succeeds?

后端 未结 8 1937
长情又很酷
长情又很酷 2020-11-30 01:20

urllib.urlretrieve returns silently even if the file doesn\'t exist on the remote http server, it just saves a html page to the named file. For example:

8条回答
  •  臣服心动
    2020-11-30 01:49

    class MyURLopener(urllib.FancyURLopener):
        http_error_default = urllib.URLopener.http_error_default
    
    url = "http://page404.com"
    filename = "download.txt"
    def reporthook(blockcount, blocksize, totalsize):
        pass
        ...
    
    try:
        (f,headers)=MyURLopener().retrieve(url, filename, reporthook)
    except Exception, e:
        print e
    

提交回复
热议问题