如何通过一个url链接下载文件到指定位置

假如一个url是http://xx.xx.xx.xx:0000/test/?path=/test/test/test.txt

  • Linux系统可以使用curl命令
curl -O url

因此也可以在其他语言比如python,java里调用linux系统运行curl

  • 浏览器可直接放在搜索框,浏览器自带下载链接会下载
  • python里可以用requests包
import requests
import os

locat = "/test/test/test.txt"
url   = "http://xx.xx.xx.xx:0000/test/?path=" + locat
file1 = "/test/test/test.txt"
try:
    res = requests.get(url)
    res.raise_for_status()
    with open(file1,'wb') as f:
        f.write(res.content)
        f.close
except:
    print('error!')
  • 结合tkinter
import requests
import os
from tkinter import *
import tkinter.font as tf

def btn_click():

    # 调整字体高亮
    ft = tf.Font(family='微软雅黑',size=10) 
    text.tag_config('tag1',foreground = 'blue',background='pink',font = ft)
    ft = tf.Font(family='微软雅黑',size=10) 
    text.tag_config('tag2',foreground = 'red',background='pink',font = ft)
    ft = tf.Font(family='微软雅黑',size=10) 
    text.tag_config('tag3',foreground = 'green',font = ft)
    
    locat = ipText.get()
	url   = "http://xx.xx.xx.xx:0000/test/?path=" + locat
    try:
        filenm= url[url.index('浙'):]
        chepai= url[url.index('浙'):-12]
        path1 = os.path.join(os.path.expanduser("~"),'Desktop') + '\\系统图片\\'
        file1 = path1 + filenm
        try:
            if not os.path.exists(path1):
                text.insert(INSERT,"在您的桌面创建了系统图片文件夹以存储下载图片\n")
                os.mkdir(path1)
            if not os.path.exists(file1):
                res = requests.get(url)
                res.raise_for_status()
                with open(file1,'wb') as f:
                    f.write(res.content)
                    f.close
                    text.insert(INSERT,"您选择的%s图片下载成功!!!\n" % chepai,'tag3')
            else:
                text.insert(INSERT,"您选择的%s图片已存在!!!\n" % chepai,'tag1')
        except:
            text.insert(INSERT,"您选择的%s图片下载失败!!!请确定下载链接和网络是否正确\n" % chepai,'tag2')
    except:
        text.insert(INSERT,"请输入正确的包含时间路径等来源于pic表的图片存储路径信息\n","tag2")
        text.insert(INSERT,"类似于/data/image/node1/202005/25/12/G00000000000000_浙GXXXXX_0_image.jpg\n")


# enter调用
def btn_click_enter(self):
    btn_click()

# 清空消息 
def cleartext():
    text.delete('0.0', END)
    
# 创建窗口对象的背景色
root = Tk()
root.title('便捷式一键图片下载服务')
root.geometry('920x700')

# Frame为布局函数
main_frame = Frame(root)
text_frame = Frame(main_frame)
station_frame = Frame(main_frame)
botton_frame = Frame(station_frame)
# 建立列表 
l1 = Label(station_frame,text='输入图片全路径')
#l2 = Label(station_frame,text='')
ipText=Entry(station_frame)
# 字体显示
# ft = tkFont.Font(family='Fixdsys', size=10, weight=tkFont.BOLD)
# pack是加载到窗口
l1.pack(side='left')
ipText.pack(side='left')
ipText['width']=70
#l2.pack(side='left')

'''
两个函数的意义是既能enter运行,又可以点击运行,方便操作,扩大使用
bind绑定enter键
注意里面是return 而不是enter
'''
b = Button(station_frame,text='查询',command=btn_click)
b['width']=4
b['height']=1
b.pack(side='left')
ipText.bind("<Return>", btn_click_enter)

# 消息输入界面
text = Text(text_frame,width = 119, height= 39)
text.pack()
main_frame.pack()
c = Button(text='清空',command=cleartext)
c['width']=4
c['height']=1
c.pack(side='left')

# 输入框的位置
station_frame.pack(side='top',pady='10')
text_frame.pack()

# 进入消息循环
root.mainloop()  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章