结合tkinter与opencv爬取豆瓣电影(2

先看下效果^ ^

嘻嘻

注:本片为上一篇文章实现爬取豆瓣电影的优化版

除源代码外,其他皆与结合tkinter与opencv爬取豆瓣电影(1 相同
直接附上源代码:

from tkinter import *
import tkinter as tk
from tkinter import ttk
import cv2
import os
import requests
from bs4 import BeautifulSoup
import re
import urllib.request,urllib.error
import PIL.Image,PIL.ImageTk
from PIL import Image, ImageTkclass Douban:def __init__(self):self.camera1 = None self.camera2=Noneself.root = Tk()self.root.title('douban.movie')self.root.geometry('1000x800')self.root.iconbitmap('0.gif')self.flag=Falseself.len=0self.first=Trueself.root.configure(bg='yellow')self.Douban_info()mainloop()def Douban_info(self):global button3self.Image_info1=Label(self.root,bg='red')self.Image_info1.place(x=700,y=350)self.Image_info2=Label(self.root,width=500,height=500,bg='red')self.Image_info2.place(x=180,y=290)self.url="".join(Data_info[i][1])self.camera2=cv2.VideoCapture(self.url)self.text=Text(self.root,width=25,height=15)self.text.place(x=0,y=350)Label(self.root,text='预告片信息列表', fg='green', bg='yellow',font=('宋体', 15)).place(x=23,y=320)Label(self.root,text='海 报 图 片', fg='purple', bg='white',font=('宋体', 18)).place(x=780,y=315)self.Movie_list()self.Basic_info()self.Poster_info()self.button1=Button(self.root, width=8, height=2, text="上一部", bg="orange",font=("楷", 12),command=self.Prior_film).place(x=0, y=550)self.button2=Button(self.root, width=8, height=2, text="下一部", bg="orange",font=("楷", 12),command=self.Next_film).place(x=90, y=550)       self.button3=Button(self.root, width=18, height=2, text="播放(暂停)", bg="blue",fg="white",font=("宋", 12),command=self.Start)self.button3.place(x=0, y=650)self.button4=Button(self.root, width=18, height=2, text="退出播放", bg="blue",fg="white",font=("宋", 12),command=self.Exit)self.button4.place(x=0, y=700)self.button5=Button(self.root, width=18, height=2, text="倍速播放", bg="blue",fg="white",font=("宋", 12),command=self.Speed)self.button5.place(x=0, y=600)def Next_film(self):global iif i>=len(Data_info):returnelse:i+=1self.len=0self.first=Trueself.camera2.release()self.url="".join(Data_info[i][1])self.camera2=cv2.VideoCapture(self.url)self.Basic_info()self.Image_info1.place_forget()self.Poster_info()def Prior_film(self):global iif i<=0:returnelse:i-=1self.len=0self.first=Trueself.camera2.release()self.url="".join(Data_info[i][1])self.camera2=cv2.VideoCapture(self.url)self.Basic_info()self.Image_info1.place_forget()self.Poster_info()def Basic_info(self):self.text.delete('1.0','end')self.text.tag_config("tag_1", backgroun="yellow", foreground="red")self.text.tag_config("tag_2", backgroun="white", foreground="blue")self.text.insert(END,"电影名: ","tag_1")self.text.insert(END,str(Data_info[i][4])+"\n"+"\n","tag_2")self.text.insert(END,"导演: ","tag_1")self.text.insert(END,str(Data_info[i][5])+"\n"+"\n","tag_2")self.text.insert(END,"主演: ","tag_1")actors=Data_info[i][0]for actor in actors:self.text.insert(END,str(actor)+"  ","tag_2")self.text.insert(END,"\n"+"\n") self.text.insert(END,"预告片链接:","tag_1")self.text.insert(END,str(Data_info[i][3])+"\n"+"\n","tag_2")self.text.insert(END,"海报链接:","tag_1")self.text.insert(END,str(Data_info[i][2])+"\n"+"\n","tag_2")def Save_poster(self,URL):response=requests.get(URL)Poster=os.path.join("D:/","poster")self.File_path='{0}/{1}.{2}'.format(Poster,str(Data_info[i][4]),'jpg')if not os.path.exists(self.File_path):with open(self.File_path,'wb')as f:f.write(response.content)def Save_video(self,URL):flag=Trueresponse = requests.get(URL)Video = os.path.join("D:/","video")self.File_path='{0}/{1}.{2}'.format(Video,str(Data_info[i][4]),'mp4')if not os.path.exists(self.File_path):with open(self.File_path,'wb')as f:flag=Falsetk.messagebox.showinfo(title='下载电影', message='开始下载电影')root=Tk()root.geometry("800x150")root.title('下载')Label(root,text='电影正在下载中', fg='red', bg='yellow',font=('宋体', 30)).place(x=190,y=60)cv=Canvas(root,bg='white',width=700,height=20)cv.place(x=50,y=120)cv=Canvas(root,bg='Lime',width=0,height=20)cv.place(x=50,y=120)for j in range(0,700):cv['width']=jroot.update()f.write(response.content)root.destroy()if flag:tk.messagebox.showinfo(title='下载电影', message='文件中已存在该电影')else:tk.messagebox.showinfo(title='下载电影', message='电影下载完毕')def Poster_info(self):self.Image_info2.place_forget()self.Image_info1.place(x=700,y=350)self.url1="".join(Data_info[i][2])self.Save_poster(self.url1)self.camera1=cv2.VideoCapture(self.url1)Success,img=self.camera1.read() if Success:cv2image=cv2.cvtColor(img,cv2.COLOR_BGR2RGBA)Current_image=Image.fromarray(cv2image) imgtk=ImageTk.PhotoImage(image=Current_image)self.Image_info1.imgtk=imgtk self.Image_info1.config(image=imgtk)def Film_info(self):self.Save_video(self.url)self.Loop_film()def Loop_film(self):cv1=Canvas(self.root,bg='white',width=790,height=10)cv1.place(x=180,y=770)self.cv=Canvas(self.root,bg='Lime',width=0,height=10)self.cv.place(x=180,y=770)self.Image_info2.place(x=180,y=290)frame_count=self.camera2.get(7)rate=self.camera2.get(5)step=790/frame_countself.len+=stepif self.len<=790:self.cv['width']=self.lenSuccess,img=self.camera2.read() if Success:cv2image=cv2.cvtColor(img,cv2.COLOR_BGR2RGBA)if not self.flag:return Current_image=Image.fromarray(cv2image) imgtk=ImageTk.PhotoImage(image=Current_image)self.Image_info2.imgtk=imgtk self.Image_info2.config(image=imgtk)self.root.after(24,lambda:self.Loop_film())def Exit(self):self.cv.place_forget()self.camera2.release()self.Image_info2.place_forget()self.url="".join(Data_info[i][1])self.camera2=cv2.VideoCapture(self.url)self.root.update()def Speed(self):self.url="".join(Data_info[i][1])self.camera2=cv2.VideoCapture(self.url)self.flag=Trueself.Loop_film()def Start(self):if self.first:self.flag= not self.flagself.button3['text']='暂停'self.Film_info()self.root.update()self.first=Falseelse:self.flag= not self.flagif self.flag:self.button3['text']='暂停'self.Loop_film()self.root.update()else:self.button3['text']='播放'self.root.update()def Movie_list(self):Label(self.root,text='豆  瓣  电  影', fg='red', bg='yellow',font=('宋体', 30)).place(x=380,y=30)self.checkDate=ttk.Treeview(self.root,column=('name'))self.checkDate.heading('#0',text='电影序号')self.checkDate.heading('name',text='电影名称')self.checkDate.column('name',width=800,anchor='center') c=list(range(1,len(Data_info)+1)) d=[]for index in range(0,len(Data_info)):d.append(Data_info[index][4])dict1 = dict(zip(c,d))rowCount=1self.checkDate.tag_configure("evenColor",background="LightBlue")for index in dict1.keys():if rowCount%2==0:self.checkDate.insert("",'end',text='                  '+str(index), values=dict1[index])else: self.checkDate.insert("",'end',text='                  '+str(index), values=dict1[index],tags=("evenColor"))rowCount+=1yscrollbar = Scrollbar(self.root,orient=VERTICAL,command=self.checkDate.yview)self.checkDate.configure(yscrollcommand=yscrollbar.set)yscrollbar.pack(side=RIGHT,fill=Y)self.checkDate.place(x=0,y=80)def Click(event):global iself.camera2.release()self.flag=Truei=int(self.checkDate.selection()[0][2:4])-1self.url="".join(Data_info[i][1])self.camera2=cv2.VideoCapture(self.url)self.Loop_film()self.Poster_info()self.checkDate.bind('<<TreeviewSelect>>',Click)class Clutch():def __init__(self):self.findLink=re.compile(r'<a class="ticket-btn" data-psource="poster" href="(.*?)" target="_blank">')self.findImag=re.compile(r'<img.*?src="(.*?)".*?>',re.S)self.findTitle=re.compile(r'<span property="v:itemreviewed">(.*)</span>')self.finddirector=re.compile(r'<a href=".*?" rel="v:directedBy">(.*)</a>')self.findActor=re.compile(r'<a\b href="[^"]*"[^>]*>([\s\S]*?)</a>',re.S)self.findVideo=re.compile(r'<a\b[^>]+\bhref="([^"]*)"[^>]*>[\s\S]*?</a>',re.S)self.findRealVideo=re.compile(r'<source src="(.*?)".*?>')Douban_url="https://movie.douban.com/cinema/nowplaying/beijing/"List_url=self.Get_url(Douban_url)Data_info=self.Crawing(List_url)def Get_url(self,Douban_url):List_url=[]    html=self.Ask_url(Douban_url)soup=BeautifulSoup(html,"html.parser")  for item in soup.find_all("li",class_="poster"):item=str(item)link=re.findall(self.findLink,item)List_url.append(link)return List_urldef Crawing(self,List_url):for i in range(0,30):print("爬虫中……")url="".join(List_url[i])html=self.Ask_url(url)soup=BeautifulSoup(html,"html.parser")for item in soup.find_all('div',id="wrapper"):data=[]actors=[]for item1 in item.find_all('a',rel="v:starring"):item1=str(item1)actor=re.findall(self.findActor,item1)actors.append("".join(actor))data.append(actors)for item2 in item.find_all('li',class_="label-trailer"):item2=str(item2)video=re.findall(self.findVideo,item2)urlvideo="".join(video)htmlvideo=self.Ask_url(urlvideo)soup_video=BeautifulSoup(htmlvideo,"html.parser")for V in soup_video.find_all('div', class_="cont"):V=str(V)real_video=re.findall(self.findRealVideo,V)data.append("".join(real_video))for item3 in item.find_all('div',class_="subject clearfix"):item3=str(item3)img=re.findall(self.findImag,item3)data.append("".join(img))item=str(item)data.append(url)titles=re.findall(self.findTitle,item)data.append("".join(titles))     director=re.findall(self.finddirector,item)data.append("".join(director))    Data_info.append(data)           return Data_infodef Ask_url(self,url):headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.47" }request=urllib.request.Request(url,headers=headers)html=" "try:response=urllib.request.urlopen(request)html=response.read().decode("utf-8")except urllib.error.URLError as e:if hasattr(e, "code"):print(e.code)if hasattr(e, "reason"):print(e.reason)return html
if __name__ == '__main__':i=0Data_info=[]Clutch=Clutch() Douban=Douban()

最后

如果你学到了知识,请毫不犹豫地点赞关注;如果你有问题,请毫不犹豫地在评论区说明你的问题,我来解决;如果你发现我有问题,那太好了,免费帮你查找各类电子书!

结合tkinter与opencv爬取豆瓣电影(2相关推荐

  1. 结合tkinter与opencv爬取豆瓣电影

    结合tkinter与opencv爬取豆瓣电影(1 前言:练习python爬虫技术 文章声明:广搜多方资源,如有侵权,请立即联系本人修改 -------------------------- 一.学习前 ...

  2. 爬取豆瓣电影排名前250部电影并且存入Mongo数据库

    2019独角兽企业重金招聘Python工程师标准>>> 需求:爬取豆瓣电影top250(https://movie.douban.com/top250)的电影数据: 标题(title ...

  3. python爬取豆瓣电影top250_Python爬虫 - scrapy - 爬取豆瓣电影TOP250

    0.前言 新接触爬虫,经过一段时间的实践,写了几个简单爬虫,爬取豆瓣电影的爬虫例子网上有很多,但都很简单,大部分只介绍了请求页面和解析部分,对于新手而言,我希望能够有一个比较全面的实例.所以找了很多实 ...

  4. day02:requests爬取豆瓣电影信息

    一.requests爬取豆瓣电影信息 -请求url: https://movie.douban.com/top250 -请求方式: get -请求头: user-agent cookies二.sele ...

  5. Requests库实战(三)---爬取豆瓣电影详细信息

    完整代码 爬取豆瓣电影的详细信息 地址:豆瓣电影动画 向下滑动时新增的数据也是Ajax请求,原理和上一个项目是一样的.唯一的不同是此处请求url携带了多个参数 import requests impo ...

  6. python爬虫慕课网利用xpath_python爬虫实践——零基础快速入门(二)爬取豆瓣电影...

    爬虫又称为网页蜘蛛,是一种程序或脚本. 但重点在于,它能够按照一定的规则,自动获取网页信息. 爬虫的基本原理--通用框架 1.挑选种子URL: 2.讲这些URL放入带抓取的URL列队: 3.取出带抓取 ...

  7. python如何爬取豆瓣_Python实战之如何爬取豆瓣电影?本文教你

    爬虫又称为网页蜘蛛,是一种程序或脚本. 但重点在于,它能够按照一定的规则,自动获取网页信息. 爬虫的基本原理--通用框架 1.挑选种子URL: 2.讲这些URL放入带抓取的URL列队: 3.取出带抓取 ...

  8. python爬取豆瓣电影top250_用Python爬虫实现爬取豆瓣电影Top250

    用Python爬虫实现爬取豆瓣电影Top250 #爬取 豆瓣电影Top250 #250个电影 ,分为10个页显示,1页有25个电影 import urllib.request from bs4 imp ...

  9. python爬取豆瓣电影信息数据

    题外话+ 大家好啊,最近自己在做一个属于自己的博客网站(准备辞职回家养老了,明年再战)在家里 琐事也很多, 加上自己 一回到家就懒了(主要是家里冷啊! 广东十几度,老家几度,躲在被窝瑟瑟发抖,) 由于 ...

最新文章

  1. 集成学习--百面机器学习
  2. 网宿科技实控人刚公布减持计划 机构席位蜂拥砸盘
  3. RocketMQ-初体验RocketMQ(07)-使用API操作RocketMQ_顺序消息 ordermessage
  4. 白话Elasticsearch53-深入聚合数据分析之Collect Model_bucket优化机制:深度优先、广度优先
  5. windows下解决pip安装出错问题
  6. 使用一个程序同时启动多个程序(c#)
  7. 鲜为人知的软件项目管理原则
  8. oracle 模拟 mysql,mysql通过表和function模拟oracle的sequence
  9. 微软采纳我的建议在WES7增加媒体中心组件
  10. Hankson 的趣味题
  11. 网络安全基础(十四)
  12. 怎么用python画世界地图_用Python画世界地图
  13. 世界上首先实现存储的电子数字计算机,世界上首先实现存储程序的电子数字计算机是ENIAC。...
  14. GreenPlum-6 最简步骤安装
  15. 红警ol服务器维护,红警OL基地升级条件汇总 腾讯红警OL手游基地升级表
  16. SwinUnet官方代码训练自己数据集(单通道灰度图像的分割)
  17. mysql 除数为0显示100,保留两位小数,显示百分号的相关操作
  18. php格式转换成docx,如何在PHP中修改.doc或.docx文件
  19. 利用闲置的树莓派4B搭建一个NAS(一)
  20. 【富文本】安装CDR时提示:Corel Graphics-Windows Shell Extension以及CDR不显示缩略图的解决方案

热门文章

  1. java byte 拓展_Java字节码文件的扩展名是( )。
  2. 杨钦鸿:支付宝开放平台开发助手安装已中止,安装程序并未成功地运行完成该如何处理?
  3. C实现猫吃老鼠问题。。。。。
  4. BosonNLP试用
  5. html5 drawimage 不显示,canvas的drawImage无法显示图像
  6. 泰坦尼克号python存活率预测--学习笔记
  7. Oracle nvl和nvl2函数
  8. linux查看目录文件大小命令
  9. Java连接MySql数据库实现增删改查功能
  10. python数据分析之matplotlib库(下)