用python(kivy模块)开发安卓影视App——搜索页面开发(二)

2024-02-25 20:50

本文主要是介绍用python(kivy模块)开发安卓影视App——搜索页面开发(二),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

由于kivy在国内使用较少,在开发道路上困难重重,本人也花了不少精力来进行开发,希望大家能够从中得到学习与锻炼。

下面是我的成果图

 首先是py文件

import requests
import re
from kivy.properties import StringProperty
from kivymd.app import MDApp
from kivymd.uix.button import MDRectangleFlatButton
from kivymd.uix.card import MDCard
from kivymd.uix import bottomsheet
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.stacklayout import MDStackLayout
from kivy.uix.screenmanager import Screen
from kivymd.font_definitions import theme_font_styles
from kivy.core.text import LabelBase
from kivymd.uix.screen import MDScreen
from kivy.core.window import Window
Window.size = (Window.height*3/4, Window.width*9/10)
screen_num = 0class DealData():class_name = ['动漫', '追剧', '电影']class_key = ['4', '2', '1']class_url = 'https://saohuo.la/list/[].html'header = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36 Edg/94.0.992.50",'Cookie': 'PHPSESSID=6rag7kboi7414tmm4amgebaonq; Hm_lvt_176de524c092a1d4ae22a38102515b0c=1634174369,1634204939,1634692218; Hm_lpvt_176de524c092a1d4ae22a38102515b0c=1634710419','Connection': 'keep-alive','Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9','Accept-Encoding': 'gzip, deflate, br','Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6','Origin': 'https://play.hhplayer.com','Referer': '','Content-type': 'application/x-www-form-urlencoded; charset=UTF-8','sec-ch-ua': '"Chromium";v="94", "Microsoft Edge";v="94", ";Not A Brand";v="99"',}class_list = '<ul class="v_list">(.*?)</ul></section><section class="page">'class_list_name = '<li><div class="v_img"><a href=".*?" title="(.*?)"><img class='class_list_picture = '<img class="lazyload" alt=".*?" data-original="(.*?)" /></a><div class='class_list_href = '<li><div class="v_img"><a href="(.*?)" title=".*?"><img class='class_list_text = '<div class="v_note">(.*?)</div></div><p class='search_url = 'https://saohuo.la/search.php?page=1&searchword=KEYWORDS&searchtype='search_list = '<ul class="v_list">(.*?)</ul></section><section class="page">'search_video_name = '<li><div class="v_img"><a href=".*?" title="(.*?)"><img class='search_video_url = '<li><div class="v_img"><a href="(.*?)" title=".*?"><img class='search_video_pic = '<img class="lazyload" data-original="(.*?)" /></a><div class="v_note">'search_video_data = '<div class="v_note">(.*?)</div>'video_class_data = '<ul class="from_list">([\s\S]*?)</ul>'video_class_name = '<li class=".*?">([\s\S]*?)</li>'video_play_data = '<section class="grid_box">([\s\S]*?)</section>'video_play_list = '<ul class="play_list"([\s\S]*?)</ul>'video_play_name = '<a title="[\s\S]*?" href="[\s\S]*?" target="_self">([\s\S]*?)</a>'video_play_url = '<a title="[\s\S]*?" href="([\s\S]*?)" target="_self">[\s\S]*?</a>'video_real_url = '><iframe src="([\s\S]*?)" width="100%" scrolling="no" height="250"'name_head = {'name': '骚火','href_head': 'https://saohuo.la', 'image_head': ''}# 首页影视分类列表
class MovieLayoutData(MDStackLayout):pass# 屏幕切换列表class MovieLayout(MDStackLayout):passclass MovieLayoutVideoData(MDBoxLayout):pass# 首页分类布局
class TabCard(MDCard):screen = StringProperty()text = StringProperty()def on_press(self):# 切换影视分类self.parent.parent.parent.parent.parent.parent.parent.parent.ids.screen_manager.current = self.screenclass TabCardCard(MDCard):screen = StringProperty()text = StringProperty()def on_press(self):print(self.screen)self.parent.parent.parent.parent.ids.video_screen_manager.current = self.screenself.parent.parent.parent.parent.screen = int(self.screen.replace('video_screen', ''))# 影视详情单class ElementCard(MDCard):url_all_datas = DealData()image = StringProperty()title_name = StringProperty()pictext = StringProperty()video_href = StringProperty()global screen_numdef on_release(self):# 切换至搜索页面global screen_numsearchscreen = MDScreen(name=f'search_screen{str(screen_num)}')list_movies = SearchScreen()list_movies.searchkey = self.title_namelist_movies_list = list_movies.add_movie_list()searchscreen.add_widget(list_movies_list)self.parent.parent.parent.parent.parent.parent.parent.parent.parent.parent.ids.home_screen_manager.add_widget(searchscreen)self.parent.parent.parent.parent.parent.parent.parent.parent.parent.parent.ids.home_screen_manager.current = f'search_screen{str(screen_num)}'screen_num += 1
# 搜索影视详情单class ElementCardCard(MDCard):url_all_datas = DealData()image = StringProperty()title_name = StringProperty()pictext = StringProperty()video_href = StringProperty()def on_press(self):global screen_numvideoscreen = MDScreen(name=f'video_play_screen{str(screen_num)}')video_play_layout = VideoPlayScreen()video_play_layout.title_name = self.title_namevideo_play_layout.video_href = self.video_hrefvideo_play_layout.image = self.imagevideo_play_layout.pictext = self.pictextvideo_play_layout.datas = []video_play_layout.pa_class_list()video_play_layout.add_class_list()videoscreen.add_widget(video_play_layout)self.parent.parent.parent.parent.parent.parent.parent.ids.home_screen_manager.add_widget(videoscreen)self.parent.parent.parent.parent.parent.parent.parent.ids.home_screen_manager.current = f'video_play_screen{str(screen_num)}'screen_num += 1class ElementCardCardCard(MDCard):image = StringProperty()title_name = StringProperty()pictext = StringProperty()video_href = StringProperty()# 主屏幕
class MainScreen(MDScreen):url_all_datas = DealData()num = 0def __init__(self):self.one_screen()self.add_class_name()def one_screen(self, **kwargs):super(MainScreen, self).__init__(**kwargs)# 主页分类标签列表def add_class_name(self):for i in range(len(self.url_all_datas.class_name)):header = TabCard()header.text = self.url_all_datas.class_name[i]header.screen = f'screen{i}'self.ids.movie_class.add_widget(header)self.add_screen_details()self.num = self.num+1# 切换主页分类def add_screen_details(self):screen = Screen(name=f'screen{str(self.num)}')screen.add_widget(self.add_movie_list())self.ids.screen_manager.add_widget(screen)# 主页分类列表def add_movie_list(self):movie_layout = MovieLayoutData()datas = self.pa_shou_ye()for data in datas:video_list = ElementCard()video_list.title_name = data['title']video_list.video_href = data['list_url']video_list.image = data['image']video_list.pictext = data['pictext']movie_layout.ids.movie_list_data.add_widget(video_list)return movie_layout# 爬取主页分类详情def pa_shou_ye(self):all_datas = []url = self.url_all_datas.class_url.replace('[]', self.url_all_datas.class_key[self.num])response = requests.get(url, headers=self.url_all_datas.header, timeout=6)html = response.content.decode('utf-8')try:list_item = re.findall(self.url_all_datas.class_list, html)[0]title_lists = re.findall(self.url_all_datas.class_list_name, list_item)href_lists = re.findall(self.url_all_datas.class_list_href, list_item)src_lists = re.findall(self.url_all_datas.class_list_picture, list_item)pictext_lists = re.findall(self.url_all_datas.class_list_text, list_item)for i in range(len(title_lists)):data = dict()data['name'] = self.url_all_datas.name_head['name']data['href_head'] = self.url_all_datas.name_head['href_head']data['image_head'] = self.url_all_datas.name_head['image_head']data['title'] = title_lists[i]data['list_url'] = data['href_head']+href_lists[i]data['image'] = data['image_head']+src_lists[i]data['pictext'] = pictext_lists[i]all_datas.append(data)return all_datasexcept:return all_datasdef on_release(self):self.ids.home_screen_manager.current = 'home_class_screen'def on_press(self):global screen_numsearchscreen = MDScreen(name=f'search_screen{str(screen_num)}')list_movies = SearchScreen()list_movies.searchkey = self.ids.search_key.textlist_movies_list = list_movies.add_movie_list()searchscreen.add_widget(list_movies_list)self.ids.home_screen_manager.add_widget(searchscreen)self.ids.home_screen_manager.current = f'search_screen{str(screen_num)}'screen_num += 1class SearchScreen():searchkey = ''url_all_datas = DealData()def add_movie_list(self):movie_layout = MovieLayout()datas = self.pa_sou_suo()for data in datas:video_list = ElementCardCard()video_list.title_name = data['title']video_list.video_href = data['list_url']video_list.image = data['image']video_list.pictext = data['pictext']movie_layout.ids.movie_list.add_widget(video_list)return movie_layout# 爬取搜索页面详情def pa_sou_suo(self):all_datas = []url = self.url_all_datas.search_url.replace('KEYWORDS', self.searchkey)response = requests.get(url, headers=self.url_all_datas.header, timeout=6)html = response.content.decode('utf-8')try:list_item = re.findall(self.url_all_datas.search_list, html)[0]title_lists = re.findall(self.url_all_datas.search_video_name, list_item)href_lists = re.findall(self.url_all_datas.search_video_url, list_item)src_lists = re.findall(self.url_all_datas.search_video_pic, list_item)pictext_lists = re.findall(self.url_all_datas.search_video_data, list_item)for i in range(len(title_lists)):data = dict()data['name'] = self.url_all_datas.name_head['name']data['href_head'] = self.url_all_datas.name_head['href_head']data['image_head'] = self.url_all_datas.name_head['image_head']data['title'] = title_lists[i]data['list_url'] = data['href_head']+href_lists[i]data['image'] = data['image_head']+src_lists[i]data['pictext'] = pictext_lists[i]all_datas.append(data)return all_datasexcept:return all_datasclass VideoPlayScreen(MDBoxLayout):title_name = ''video_href = ''image = ''pictext = ''num = 0url_all_datas = DealData()datas = []screen = 0def add_class_list(self):self.ids.movie_data.title_name = self.title_nameself.ids.movie_data.video_href = self.video_hrefself.ids.movie_data.image = self.imageself.ids.movie_data.pictext = self.pictextfor i in range(len(self.datas)):video_class_header = TabCardCard()video_class_header.text = self.datas[i][0]video_class_header.screen = f'video_screen{str(i)}'self.ids.video_play_class.add_widget(video_class_header)self.add_screen_details()if self.num < len(self.datas):self.num = self.num+1def add_screen_details(self):screen = Screen(name=f'video_screen{str(self.num)}')screen.add_widget(self.add_video_play_list())self.ids.video_screen_manager.add_widget(screen)def add_video_play_list(self):video_play_layout = MovieLayoutVideoData()video_lists = self.datas[self.num][1]for i in range(len(video_lists)):btnt = MDRectangleFlatButton(text=video_lists[i], font_name='./Lishu.ttf', size_hint_x=None)btnt.bind(on_press=self.on_press)# btnt=TabCardCardCard()# btnt.text=video_lists[i]video_play_layout.ids.movie_video_list.add_widget(btnt)return video_play_layoutdef pa_class_list(self):url = self.video_hrefresponse = requests.get(url, headers=self.url_all_datas.header, timeout=6)html = response.content.decode('utf-8')try:list_class_item = re.findall(self.url_all_datas.video_class_data, html)[0]video_class_name_lists = re.findall(self.url_all_datas.video_class_name, list_class_item)list_item = re.findall(self.url_all_datas.video_play_data, html)[0]video_play_lists = re.findall(self.url_all_datas.video_play_list, list_item)for i in range(len(video_class_name_lists)):last_video_play_href_lists = []video_play_name_lists = re.findall(self.url_all_datas.video_play_name, video_play_lists[i])video_play_href_lists = re.findall(self.url_all_datas.video_play_url, video_play_lists[i])for video_play_href_list in video_play_href_lists:video_play_href_list_ = self.url_all_datas.name_head['href_head'] + \video_play_href_listlast_video_play_href_lists.append(video_play_href_list_)data = (video_class_name_lists[i], video_play_name_lists, last_video_play_href_lists)self.datas.append(data)except:passdef on_press(self, widget):play_href = self.datas[self.screen][2][self.datas[self.screen][1].index(widget.text)]get_real_href = GetRealPlayUrl()get_real_href.video_play_url = play_hrefvideo_play_real_url = get_real_href.pa_class_list()print(video_play_real_url)class GetRealPlayUrl():url_all_datas = DealData()video_play_url = ''def pa_class_list(self):url = self.video_play_urlresponse = requests.get(url, headers=self.url_all_datas.header, timeout=6)html = response.content.decode('utf-8')try:video_real_url = re.findall(self.url_all_datas.video_real_url, html)[0]return video_real_urlexcept:return '空'class MainApp(MDApp):def build(self):self.theme_cls.theme_style = 'Light'self.theme_cls.primary_palette = 'Gray'LabelBase.register(name='Lishu', fn_regular='Lishu.ttf')theme_font_styles.append('Lishu')self.theme_cls.font_styles['Lishu'] = ['Lishu', 16, False, 0.15]return MainScreen()if __name__ == '__main__':MainApp().run()

然后是kv文件

<MainScreen>:MDBoxLayout:orientation: 'vertical'MDBoxLayout:size_hint_y:.08orientation: 'vertical'MDBoxLayout:padding:[0,0,0,0]spacing:dp(10)MDIconButton:icon: 'menu'on_release:root.on_release()MDLabel:MDTextFieldRound:id:search_keynormal_color: app.theme_cls.primary_lighthint_text:'搜索'font_name: './Lishu.ttf'size_hint_x:2pos_hint: {'x':.1,'y': .2}MDIconButton:icon: 'magnify'on_release:root.on_press()MDIconButton:icon:'download'             ScreenManager:id:home_screen_managerMDScreen:name:'home_class_screen'MDBoxLayout:orientation: 'vertical'MDBoxLayout:size_hint_y:.05ScrollView:MDBoxLayout:id:movie_classadaptive_width:Truerows:1padding:[10,2,10,2] spacing:dp(5)         ScreenManager:id:screen_managerMovieLayoutData:size_hint_y:Noneheight:Window.width*1/100adaptive_height:True screen_manager:screen_manager  MovieLayout:size_hint_y:Noneheight:Window.width*1/100adaptive_height:True screen_manager:home_screen_manager  <ElementCard>:orientation: 'vertical'size_hint_x: .5size_hint_y: Noneheight:Window.height*1/4padding: dp(10)radius:[25]MDBoxLayout:orientation: 'horizontal'size_hint_y: .9AsyncImage:source:root.image MDBoxLayout:orientation: 'vertical'size_hint_y: .9MDLabel:text: root.title_namefont_size: 15bold:Truefont_name: './Lishu.ttf'halign:'center'MDLabel:text:root.pictextfont_name: './Lishu.ttf'halign:'center'font_size: 15     
<ElementCardCard>:orientation: 'vertical'size_hint_x: 1size_hint_y: Noneheight:Window.height*1/4padding: dp(10)radius:[25]MDBoxLayout:orientation: 'horizontal'size_hint_y: .9AsyncImage:source:root.image MDBoxLayout:orientation: 'vertical'size_hint_y: .9MDLabel:text: root.title_namefont_size: 24bold:Truefont_name: './Lishu.ttf'halign:'center'MDLabel:text:root.pictextfont_name: './Lishu.ttf'halign:'center'font_size: 18      
<ElementCardCardCard>:orientation: 'vertical'size_hint_x: .5size_hint_y: Noneheight:Window.height*1/4padding: dp(10)radius:[25]MDBoxLayout:orientation: 'horizontal'size_hint_y: .9AsyncImage:source:root.image MDBoxLayout:orientation: 'vertical'size_hint_y: .9MDLabel:text: root.title_namefont_size: 24bold:Truefont_name: './Lishu.ttf'halign:'center'MDLabel:text:root.pictextfont_name: './Lishu.ttf'halign:'center'font_size: 18    <TabCard>:icon:'movie-roll'size_hint: None,1radius:dp(15)MDIcon:icon:root.iconMDLabel:text: root.textfont_name:'Lishu.ttf'
<TabCardCard>:size_hint: None,1radius:dp(15)MDLabel:text: root.textfont_name:'Lishu.ttf'halign:'center'<MovieLayout>:   size_hint_y:1ScrollView:MDStackLayout:id:movie_listpadding:[10,2,10,10] spacing: '5dp'adaptive_height:True 
<MovieLayoutData>:   size_hint_y:1ScrollView:MDStackLayout:id:movie_list_datapadding:[10,2,10,10] spacing: '5dp'adaptive_height:True 
<MovieLayoutVideoData>:  ScrollView:MDStackLayout:id:movie_video_listspacing:dp(10)padding:[15,2,10,2]adaptive_height:True  
<VideoPlayScreen>:orientation: 'vertical'spacing: '10dp'padding:[10,2,10,2]MDBoxLayout:size_hint_y: Noneheight:Window.height*1/4ElementCardCardCard:id:movie_data         MDBoxLayout:size_hint_y:.1ScrollView:MDBoxLayout:id:video_play_classadaptive_width:Truerows:1padding:[15,2,10,2] spacing:dp(5)   MDFillRoundFlatButton: text:'线路'font_name: './Lishu.ttf'normal_color: app.theme_cls.primary_darkbold:True  ScreenManager:id:video_screen_managerMovieLayoutVideoData:size_hint_y:Noneheight:Window.width*1/100adaptive_height:True screen_manager:video_screen_manager

如果有朋友想直接使用该代码,需要设置一个字体文件,放在代码同一目录下,且名字命为Lishu.ttf即可直接使用,如果有朋友想和文墨一起开发,请私信我~

这篇关于用python(kivy模块)开发安卓影视App——搜索页面开发(二)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/746671

相关文章

这15个Vue指令,让你的项目开发爽到爆

1. V-Hotkey 仓库地址: github.com/Dafrok/v-ho… Demo: 戳这里 https://dafrok.github.io/v-hotkey 安装: npm install --save v-hotkey 这个指令可以给组件绑定一个或多个快捷键。你想要通过按下 Escape 键后隐藏某个组件,按住 Control 和回车键再显示它吗?小菜一碟: <template

python: 多模块(.py)中全局变量的导入

文章目录 global关键字可变类型和不可变类型数据的内存地址单模块(单个py文件)的全局变量示例总结 多模块(多个py文件)的全局变量from x import x导入全局变量示例 import x导入全局变量示例 总结 global关键字 global 的作用范围是模块(.py)级别: 当你在一个模块(文件)中使用 global 声明变量时,这个变量只在该模块的全局命名空

Hadoop企业开发案例调优场景

需求 (1)需求:从1G数据中,统计每个单词出现次数。服务器3台,每台配置4G内存,4核CPU,4线程。 (2)需求分析: 1G / 128m = 8个MapTask;1个ReduceTask;1个mrAppMaster 平均每个节点运行10个 / 3台 ≈ 3个任务(4    3    3) HDFS参数调优 (1)修改:hadoop-env.sh export HDFS_NAMENOD

认识、理解、分类——acm之搜索

普通搜索方法有两种:1、广度优先搜索;2、深度优先搜索; 更多搜索方法: 3、双向广度优先搜索; 4、启发式搜索(包括A*算法等); 搜索通常会用到的知识点:状态压缩(位压缩,利用hash思想压缩)。

hdu1240、hdu1253(三维搜索题)

1、从后往前输入,(x,y,z); 2、从下往上输入,(y , z, x); 3、从左往右输入,(z,x,y); hdu1240代码如下: #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#inc

深入探索协同过滤:从原理到推荐模块案例

文章目录 前言一、协同过滤1. 基于用户的协同过滤(UserCF)2. 基于物品的协同过滤(ItemCF)3. 相似度计算方法 二、相似度计算方法1. 欧氏距离2. 皮尔逊相关系数3. 杰卡德相似系数4. 余弦相似度 三、推荐模块案例1.基于文章的协同过滤推荐功能2.基于用户的协同过滤推荐功能 前言     在信息过载的时代,推荐系统成为连接用户与内容的桥梁。本文聚焦于

嵌入式QT开发:构建高效智能的嵌入式系统

摘要: 本文深入探讨了嵌入式 QT 相关的各个方面。从 QT 框架的基础架构和核心概念出发,详细阐述了其在嵌入式环境中的优势与特点。文中分析了嵌入式 QT 的开发环境搭建过程,包括交叉编译工具链的配置等关键步骤。进一步探讨了嵌入式 QT 的界面设计与开发,涵盖了从基本控件的使用到复杂界面布局的构建。同时也深入研究了信号与槽机制在嵌入式系统中的应用,以及嵌入式 QT 与硬件设备的交互,包括输入输出设

OpenHarmony鸿蒙开发( Beta5.0)无感配网详解

1、简介 无感配网是指在设备联网过程中无需输入热点相关账号信息,即可快速实现设备配网,是一种兼顾高效性、可靠性和安全性的配网方式。 2、配网原理 2.1 通信原理 手机和智能设备之间的信息传递,利用特有的NAN协议实现。利用手机和智能设备之间的WiFi 感知订阅、发布能力,实现了数字管家应用和设备之间的发现。在完成设备间的认证和响应后,即可发送相关配网数据。同时还支持与常规Sof

如何在页面调用utility bar并传递参数至lwc组件

1.在app的utility item中添加lwc组件: 2.调用utility bar api的方式有两种: 方法一,通过lwc调用: import {LightningElement,api ,wire } from 'lwc';import { publish, MessageContext } from 'lightning/messageService';import Ca

活用c4d官方开发文档查询代码

当你问AI助手比如豆包,如何用python禁止掉xpresso标签时候,它会提示到 这时候要用到两个东西。https://developers.maxon.net/论坛搜索和开发文档 比如这里我就在官方找到正确的id描述 然后我就把参数标签换过来