對於設定禁止下載的視訊也適用
1.提取連結
2.google瀏覽器開啟——selenium
3.找到有水印的視訊網址,去除水印——bs4
4.偽裝成手機瀏覽器下載視訊——requests
import tkinter as tk
import tkinter.messagebox
from tkinter.filedialog import askdirectory
from tkinter import *
from selenium import webdriver
from time import sleep
from bs4 import BeautifulSoup
import random
import requests
headers = {'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1 Edg/84.0.4147.105'}
def get_http():
info = t1.get(1.0, 'end')
if ' 複製' in info:
url1 = info.split('ht')[1].split(' 複製')[0]
url = 'ht' + url1
else:
url = info
print('網址提取成功')
t2.insert(END, '網址提取成功\n')
return url
def google_chrome(url):
driver = webdriver.Chrome()
driver.get(url)
sleep(3)
page = driver.page_source #打出網頁碼源
print('網頁碼源提取成功')
t2.insert(END, '網頁碼源提取成功\n')
return page
def look_pages(page):
soup = BeautifulSoup(page, 'lxml')
url_video = soup.find_all(mediatype="video")[0]
name = soup.find_all(class_="name nowrap")[0]
id = soup.find_all(class_="uid nowrap")[0]
#取出水印
url_video3 = url_video.get('src').replace('/playwm/', '/play/')
name3 = name.text
id3 = str(id).split('抖音號:')[1].split('</')[0]
print('名字+視訊網址提取成功')
t2.insert(END, '名字+視訊網址提取成功\n')
return (url_video3, name3)
def download(url_video3, name3):
tt = str(random.randrange(1, 100)) # 加入亂數,以免下載重複
video = requests.get(url_video3, headers=headers).content
path = address.get()
with open(path+'/'+f'{name3+tt}.mp4', 'wb') as f:
f.write(video)
print('下載成功')
t2.insert(END, '下載成功\n')
tkinter.messagebox.showinfo('', '下載完成')
if __name__ == '__main__':
def run():
url = get_http()
page = google_chrome(url)
(url_video3, name3) = look_pages(page)
download(url_video3, name3)
window = tk.Tk()
window.title('抖音無水印下載')
window.geometry('500x300')
l1 = Label(window, text='抖音複製連結', height=1)
l1.pack(padx=0, anchor=W)
t1 = tk.Text(window, height=3)
t1.pack() # 輸入口
def selectPath():
path1 = askdirectory()
address.insert(0, path1)
address = Entry(window)
address.place(x=0, y=70)
Button(window, text="儲存路徑", command=selectPath).place(x=145, y=70)
b1 = tk.Button(window, text='確定', font=('Arial', 12), width=10, height=1, command=(run))
b1.pack(padx=10, anchor=E)
l2 = Label(window, text='下載詳情', height=1)
l2.pack(padx=0, anchor=W)
t2 = tk.Text(window, height=20) # 輸出顯示口
t2.pack(padx=0, anchor=S)
window.mainloop()