實現搜尋視訊到播放(非原創)
阿新 • • 發佈:2020-07-23
import urllib.request
import json
import requests
import re
import tkinter as tk
import tkinter.messagebox as message
import webbrowser
class Video:
def __init__(self):
self.url_list = []
self.root = tk.Tk()
self.root.title('視訊播放器')
# self.root.geometry('+100+100')
self.root.minsize( 500, 390)
self.root.resizable(True, True)
self.label = tk.Label(self.root, text='視訊播放器', font=('', 30), width=20, height=2, relief='groove')
self.label.grid(padx=100, row=0, columnspan=2)
self.prompt_label = tk.Label(self.root, text='請輸入您想查詢的電視名稱:', font=('', 12), width=35, height=2)
self.prompt_label.grid(row =1, column=0)
self.input = tk.Entry(font=('', 15), width=25)
self.input.grid(row=1, column=1)
self.label1 = tk.Label(self.root, height=1)
self.label1.grid(row=2, columnspan=2)
self.search = tk.Button(self.root, text='搜尋', font=('', 12), width=15, height=1, command=self.search_button)
self.search.grid(row =3, column=0)
tk.Button(self.root, text='播放', font=('', 12), width=15, height=1, command=self.change_items).grid(row=3, column=1)
self.label2 = tk.Label(self.root, height=1)
self.label2.grid(row=4, columnspan=2)
self.list_box = tk.Listbox(self.root, width=15, height=10, font=('', 12), selectmode='extended')
self.list_box.grid(row=5, columnspan=2)
self.list_box.insert(0, '空目錄')
def search_movie(self, name):
html = ''
if name != 'quit':
search_url = 'https://api.okzy.tv/index.php/vod/search.html?wd={}&submit=search'.format(name) # str格式化,將內容拼接到str中
# print(search_url)
response = requests.get(search_url) # 傳送http請求,返回資訊
html = response.text # 獲取網頁內容
else:
exit()
if len(re.findall(r'<div class="text">搜尋功能關閉中</div>', html)) != 0:
message.showinfo('提示', '操作頻繁,請稍後重試')
# print("操作頻繁,請稍後重試")
elif len(re.findall(r'<h1>沒有找到匹配資料</h1>', html)) == 0: # 根據正則在html中尋找匹配的內容,並返回list
# print(html)
sp_ids = re.findall(r'<span class="xing_vb4"><a href="/index.php/vod/detail/id/(.*?).html"', html)[
0] # 獲取正則中()的內容
self.get_record(sp_ids)
else:
message.showinfo('提示', '抱歉,未能找到你想要看的視訊')
# print('抱歉,未能找到你想要看的視訊')
def get_record(self, ids_url_c):
num_list = []
# 拼接需要整理的視訊連結
url = 'http://api.okzy.tv/api.php/provide/vod/?ac=detail&ids={}'.format(ids_url_c)
response = urllib.request.urlopen(url) # 開啟url並返回網頁資料
content = response.read() # 讀取網頁內容
json_content = json.loads(content)
content2 = json_content['list'][0]['vod_play_url'] # 獲取指定的json內容
content2_list = content2.split('$$$')[0] # 0和1是不同的線路方式
content2_list = content2_list.split('#')
self.url_list.clear()
for i in range(len(content2_list)): # 獲取內容數量
url_str = content2_list[i]
str1 = url_str.split('$')
num_list.append(str1[0])
self.url_list.append(str1[1])
self.video_list(num_list)
def video_list(self, num_list):
self.list_box.delete(0, 'end')
for i in num_list:
self.list_box.insert('end', i)
def change_items(self):
indexs = self.list_box.curselection()
self.player_button(indexs) # 選中的集都播放
def player_button(self, indexs):
for i in indexs:
webbrowser.open("http://jx.sang0.com/?v={}".format(self.url_list[i]))
def search_button(self):
self.search_movie(self.input.get())
if __name__ == '__main__':
video = Video()
video.root.mainloop()
原文連結 https://jingyan.baidu.com/article/8cdccae9823e63705513cd07.html