当前位置:   article > 正文

python利用tkinter制作查询热映电影软件_python tkinter 电影

python tkinter 电影
  1. 淘票票电影热榜网址:
  2. https://dianying.taobao.com/showList.htm?spm=a1z21.6646273.city.2.4ed46d6ekOc3wH&n_s=new&city=310100

网站截图:

很多人学习python,不知道从何学起。
很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手。
很多已经做案例的人,却不知道如何去学习更加高深的知识。
那么针对这三类人,我给大家提供一个好的学习平台,免费领取视频教程,电子书籍,以及课程的源代码!??¤
QQ群:961562169

在这里插入图片描述

spider4taopiaopiao.py

爬取网站电影排行榜
  1. import requests
  2. import re
  3. import os
  4. import time
  5. import json
  6. def mySpider():
  7. # 伪装 用于可以伪装成浏览器。
  8. headers = {
  9. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'
  10. }
  11. print("网页请求中...")
  12. time.sleep(0.5)
  13. url = "https://dianying.taobao.com/showList.htm?spm=a1z21.6646273.city.2.4ed46d6ekOc3wH&n_s=new&city=310100"
  14. response = requests.get(url, headers=headers)
  15. html = response.text # 获取html信息
  16. # print(html)
  17. print("网页信息已获取...")
  18. time.sleep(0.5)
  19. destinationPath = "result.txt"
  20. fd = open(destinationPath,"w+",encoding='utf-8')
  21. fd.writelines(html)
  22. end = html.find('<!-- 即将热映 -->')
  23. # print("位置为:",end)
  24. if end != -1:
  25. html = html[:end]
  26. fd.close()
  27. s = '<img width="160" height="224" data-src="(.*?)" src=' +\
  28. '.*?<span class="bt-l">(.+?)</span>.*?<span class="bt-r">(\d.\d)?</span>' + \
  29. ".*?<span>导演:(.*?)</span>" + ".*?<span>主演:(.*?)</span>" + ".*?<span>类型:(.*?)</span>"+\
  30. ".*?<span>地区:(.*?)</span>" + ".*?<span>语言:(.*?)</span>" + ".*?<span>片长:(.*?)</span>"
  31. pattern = re.compile(s,re.S)
  32. items = re.findall(pattern, html)
  33. # print(items)
  34. # print(type(items))
  35. # print(type(html))
  36. for outer in range(len(items)):
  37. items[outer] = list(items[outer])
  38. for i in range(len(items[outer])):
  39. if items[outer][i] == "":
  40. items[outer][i] = "暂无信息"
  41. else:
  42. # pass # &middot;
  43. items[outer][i] = items[outer][i].replace("&middot;","·")
  44. # print(items)
  45. destinationPath = "items.json"
  46. fd = open(destinationPath,"w+",encoding='utf-8')
  47. json.dump(items,fd)
  48. fd.close()
  49. # 建立下载目录
  50. dir_name = "./images"
  51. if not os.path.exists(dir_name):
  52. os.mkdir(dir_name)
  53. cnt = 0
  54. for item in items:
  55. url = item[0] # 以'/'来分割字符串
  56. file_name = str(cnt) + ".jpg"
  57. cnt += 1
  58. response = requests.get(url, headers=headers)
  59. # 保存
  60. with open(dir_name + "/" + file_name, 'wb') as f:
  61. f.write(response.content) # 将图片写入到文件夹下保存
  62. info = "图片文件: {0:25}{1}".format(file_name," 成功下载...")
  63. print(info)
  64. # print(items)
  65. return items
  66. if __name__ == "__main__":
  67. # pass
  68. mySpider()

运行结果展示:
在这里插入图片描述

GUI4Spider.py

制作简易的tkinter GUI 图形化用户交互界面
  •  
  1. from spider4taopiaopiao import mySpider
  2. from tkinter import *
  3. import time
  4. from PIL import Image,ImageTk
  5. import json
  6. # sourcePath = "items.json"
  7. # fs = open(sourcePath,"r",encoding='utf-8')
  8. # items = json.load(fs)
  9. # fs.close()
  10. # print(len(items))
  11. # print(items)
  12. items = mySpider()
  13. # 0图片链接 1电影名 2评分 3导演 4主演 5类型 6地区 7语言 8片长
  14. infoMap = {
  15. 0:"图片链接:", 1:"电影名:", 2:"评分:", 3:"导演:",
  16. 4:"主演:", 5:"类型:", 6:"地区:", 7:"语言:", 8:"片长:"
  17. }
  18. current_rank = 1
  19. total_rank = len(items)
  20. root = Tk()
  21. root.title("淘票票电影热映排行榜,更新时间:"+\
  22. time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
  23. root.geometry('800x800')
  24. root.iconbitmap("movie.ico")
  25. def showPre():
  26. global current_rank,total_rank
  27. if current_rank <= 1:
  28. current_rank = 2
  29. current_rank -= 1
  30. print("显示前一部电影...",current_rank)
  31. labimgconfig()
  32. labInfoConfig()
  33. def showNxt():
  34. global current_rank,total_rank
  35. if current_rank >= total_rank:
  36. current_rank = total_rank-1
  37. current_rank += 1
  38. print("显示后一部电影...",current_rank)
  39. labimgconfig()
  40. labInfoConfig()
  41. def labimgconfig():
  42. filename = "images/" + str(current_rank-1) + ".jpg"
  43. global newImage
  44. newImage = getImage(filename)
  45. labimg.config(image=newImage)
  46. def getImage(filename):
  47. imageJPG = Image.open(filename)
  48. image = ImageTk.PhotoImage(imageJPG)
  49. return image
  50. def labInfoConfig():
  51. info = items[current_rank-1]
  52. for i in range(len(labInfo)):
  53. labInfo[i].config(text=infoMap[i+1]+info[i+1])
  54. labRank.config(text="排名:#" +str(current_rank))
  55. image = getImage("images/0.jpg")
  56. labimg = Label(root) # 设置Widget控件显示的图像
  57. labimg.config(image=image)
  58. labimg.pack() # 包装与定位组件
  59. colors = ["Red","Orange","Yellow","Green","Blue","Violet","Purple","Chocolate"]
  60. labInfo = []
  61. for color in colors:
  62. labtemp = Label(root,bg=color,width=200,height=3,wraplength=1000)
  63. labtemp.pack()
  64. labInfo.append(labtemp)
  65. labRank = Label(root,bg="Red",width=9,height=3,text="排名:#" +str(current_rank))
  66. labRank.pack()
  67. # labName = Label(root,bg="Red",width=50,height=2)
  68. # labName.pack()
  69. # labScore = Label(root,bg="Orange",width=50,height=2)
  70. # labScore.pack()
  71. # labDirector = Label(root,bg="Yellow",width=50,height=2)
  72. # labDirector.pack()
  73. # labActor = Label(root,bg="Green",width=50,height=2)
  74. # labActor.pack()
  75. # labType = Label(root,bg="Blue",width=50,height=2)
  76. # labType.pack()
  77. # labDistrict = Label(root,bg="Violet",width=50,height=2)
  78. # labDistrict.pack()
  79. # labLanguage = Label(root,bg="Purple",width=50,height=2)
  80. # labLanguage.pack()
  81. # labLength = Label(root,bg="Chocolate",width=50,height=2)
  82. # labLength.pack()
  83. btnPre = Button(root,width=15,height=5,text="显示前一个",command=showPre)
  84. btnNxt = Button(root,width=15,height=5,text="显示后一个",command=showNxt)
  85. btnPre.pack(side=LEFT,anchor=S)
  86. btnNxt.pack(side=RIGHT,anchor=S)
  87. showPre()
  88. root.mainloop()

运行结果如下:
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/687074
推荐阅读
相关标签
  

闽ICP备14008679号