@@ -97,7 +97,8 @@ def download_big_file(url, mkdir, name=""):
9797 name = os .path .join (mkdir , name )
9898
9999 start_time = time .time ()
100- with requests .get (url , stream = True , headers = {"User-Agent" : USER_AGENT }, verify = False ) as r :
100+ req = requests .get (url , stream = True , headers = {"User-Agent" : USER_AGENT }, verify = False )
101+ with req as r :
101102 content_length = int (r .headers ['content-length' ])
102103 line = 'content-length: %dB/%.2fKB/%.2fMB'
103104 print (name , line % (content_length , content_length / 1024 , content_length / 1024 / 1024 ))
@@ -112,8 +113,11 @@ def download_big_file(url, mkdir, name=""):
112113 print (name , line , end = '\r ' )
113114 if down_size >= content_length :
114115 break
115- time_cost = time .time () - start_time
116- print (name , '共耗时:%.2f s,平均速度:%.2f KB/s' % (time_cost , down_size / 1024 / time_cost ))
116+ f .close ()
117+ r .close ()
118+ req .close ()
119+ time_cost = time .time () - start_time
120+ print (name , '共耗时:%.2f s,平均速度:%.2f KB/s' % (time_cost , down_size / 1024 / time_cost ))
117121
118122
119123def download_file (url , mkdir , name = "" ):
@@ -143,9 +147,12 @@ def download_file(url, mkdir, name=""):
143147 # 判断文件是否存在
144148 # if not os.path.exists(name):
145149 if not os .path .isfile (name ):
150+ req = requests .get (url , headers = {"User-Agent" : USER_AGENT }, verify = False , timeout = 600 )
146151 # 文件不存在才保存
147152 with open (name , "wb" ) as f :
148- f .write (requests .get (url , headers = {"User-Agent" : USER_AGENT }, verify = False , timeout = 600 ).content )
153+ f .write (req .content )
154+ f .close ()
155+ req .close ()
149156 return name
150157
151158
0 commit comments