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()

实现搜索视频到播放(非原创)_分享