def down_file(url, name, path): if os.path.exists(path): return print("开始下载:" + name + ".mp3") headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', "Accept-Encoding": "gzip, deflate, br", "Accept-Language": "zh-CN,zh;q=0.9", "Upgrade-Insecure-Requests": "1", 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'} count = 0 while count < 3: try: r = requests.get(url, headers=headers, stream=True, timeout=60) # print(r.status_code) if (r.status_code == 200): with open(path, "wb+") as f: for chunk in r.iter_content(1024): f.write(chunk) print("完成下载:" + name + ".mp3") break except Exception as e: print(e) print("下载出错:" + name + ".mp3,3秒后重试") if os.path.exists(path): os.remove(path) time.sleep(3) count += 1 pass