#coding:utf-8 import requests from contextlib import closing #文件下載器 def Down_load(file_url,file_path): headers = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"} with closing(requests.get(file_url,headers=headers,stream=True)) as response: chunk_size = 1024 # 單次請求最大值 content_size = int(response.headers['content-length']) # 內容體總大小 data_count = 0 with open(file_path, "wb") as file: for data in response.iter_content(chunk_size=chunk_size): file.write(data) data_count = data_count + len(data) now_jd = (data_count / content_size) * 100 print("\r 文件下載進度:%d%%(%d/%d) - %s" % (now_jd, data_count, content_size, file_path), end=" ") if __name__ == '__main__': headers = { "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36", } file_url = 'https://down.360safe.com/se/360se8.1.1.404.exe' #文件鏈接 file_path = "D:\\demo/002.exe" #文件路徑 Down_load(file_url,file_path)