欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python下载文件

程序员文章站 2022-04-09 07:50:28
...

Python下载文件

from requests import get
from time import time
from tkinter import Tk, Label, Entry, Button, N, E, W

def getu(url):
    with get(url, stream=True) as r:
        print("正在检查资源大小")
        if(r.headers.get('Content-Length')):
            b = int(r.headers.get('Content-Length'))/1024/1024
        else:
            b = 0
        print("资源大小",b, "MB")

        print('-' * 32)
        print("正在准备")
        chunk_siz = 1024

        print('-' * 32)
        print("正在下载资源")
        c = url.split(".")
        a = time()
        with open('test.'+c[len(c)-1], "wb") as code:
            for chunk in r.iter_content(chunk_size=chunk_siz):
                code.write(chunk)
                code.flush()
            code.close()
        a = time()-a
        if(a != 0):
            print("平均写入速度:约", round(b/a, 1), "MB/秒")
        else:
            print("平均写入速度:约 0 MB/秒")
        print('-' * 32)

def getuo():
    if(e1.get()): getu(e1.get())

win = Tk()
win.title("下载器")
l1 = Label(win, text="下载文件网址位置:")
e1 = Entry(win, exportselection=0)
b1 = Button(win, command=getuo, text="下载", bg="red")
l1.grid(row=0, column=0)
e1.grid(row=0, column=1)
b1.grid(row=1, columnspan=2, sticky=N+E+W)
win.resizable(0,0)
win.mainloop()
相关标签: python

上一篇: 没有知觉了

下一篇: word 转 PDF