python 下载文件
2020-02-21 python 3849
import requests import time import os import base64, re def downloader(url, path): start = time.time() size = 0 response = requests.get(url, stream=True) # stream 必须带上 chunk_size = 1024 # 每次下载大小 if "content-length" in response.headers: flag = 1 content_size = int(response.headers['content-length']) else: flag = 0 content_size = 1024 if response.status_code == 200: if flag == 1: print("[文件大小]:%.4f MB" % (content_size / chunk_size / 1024)) with open(path, "wb") as file: for data in response.iter_content(chunk_size=chunk_size): file.write(data) size += len(data) # 已下载大小 t = time.time() - start if t > 0: kbs = round(size / round(time.time() - start, 3) / 1024, 2) # \r 指定第一个字符开始,搭配end属性完成覆盖进度条 print("\r" + "[下载进度]:%s%.2f%% %.2fkb/s" % ( ">" * int(size * 50 / content_size), float(size / content_size * 100), kbs), end="") if flag == 0: print("[文件大小]:%.4f MB" % (os.path.getsize(path) / chunk_size / 1024)) end = time.time() # 结束时间 print("\n" + "全部下载完成!用时%.2f秒" % (end - start)) # if __name__ == "__main__": # url = "http://d2.zxcs6.xyz/20/zzx,cuslaa.rar" # downloader(url=url,path="宰执天下.rar") def url_decode(url_user): url_encode = url_user.lstrip('thunder://').encode() link = base64.b64decode(url_encode).decode('gbk') link = link.strip('AZ') return link def url_parse(thunder): if re.match('thunder://', thunder): return url_decode(thunder) else: return None def url_chenk(url_user): while True: if re.match('thunder://', url_user): break else: print('你输入的不是需要破解的迅雷链接。\n') url_user = input('请重新输入迅雷下载地址:\n') return url_user # thunder = "thunder://QUFodHRwczovL21teHpxeGwxLmNvbS9jb21tb24vYWxsLzIwMTgxMi8yeTRoN21uNC8yeTRoN21uNC5ybXZiWlo=/" # url = xunlei.url_parse(thunder)
很赞哦! (0)
相关文章
文章评论
-
-
-
0条评论