如何通過一個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()  

 

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