python3 retrieve(),,鏍囩锛?a hre
python3 retrieve(),,鏍囩锛?a hre
鏍囩锛?a href='http://www.byrx.net/so/1/ESS' title='ESS'>ESS
retreive()琚涓烘槸涓€佹棫鍑芥暟锛屽彲鑳戒細鍦ㄥ皢鏉ユ姏寮冦€?br/>urllib.request.urlretrieve(url, filename=None, reporthook=None, data=None)Exceptions鍜屽嚱鏁皍rlopen()鐩稿悓銆?br/>鍏蜂綋鏂囨。锛屽弬瑙?a href="https://docs.python.org/3/library/urllib.request.html#module-urllib.request" rel="nofollow">https://docs.python.org/3/library/urllib.request.html#module-urllib.request
浠g爜鐗囨
from urllib.request import urlretrieveimport socketimport osimport sysdef reporthook(blocknum, bs, size): # blocknum:宸茬粡涓嬭浇鐨勬暟鎹潡 bs:鏁版嵁鍧楃殑澶у皬 size:杩滅▼鏂囦欢鐨勫ぇ灏? per = 100.0 * blocknum * bs / size if per > 100 : per = 100 sys.stdout.write(鈥楧ownload progress: %.2f%% \r鈥?per) sys.stdout.flush()class ClassName(): def __init__(self): socket.setdefaulttimeout(30) def run(self): ... while True: ..... try: urlretrieve(url,localfile, reporthook= reporthook) except socket.timeout: self.redownload(url,localfile) except Exception as e: StatusCode=e.getcode() if StatusCode == 404: print(e) break self.redownload(url,localfile) def redownload(self,url,localfile): count = 1 while count <= 5: try: urlretrieve(url,localfile, reporthook= reporthook) break except socket.timeout: err_info = 鈥楻eloading for %d time.....鈥?count if count == 1 else 鈥楻eloading for %d times.....鈥?count print(err_info) count += 1 if count > 5: print("download job failed!")
python3 retrieve()
鏍囩锛?a href='http://www.byrx.net/so/1/ESS' title='ESS'>ESS
鍘熸枃鍦板潃锛歨ttps://blog.51cto.com/whbill/2483367
评论关闭