Python-一键获取电影资源链接实例解析(requests、正则表达式、GUI)
2020-01-14 02:28
489 查看
''' 本代码实现了一键获取80s手机电影网的电影下载连接 用户可以通过下拉框进行电影种类的筛选,比如国语、动作、2019、美国等等 实现本代码主要用到了Pyhton中的以下知识点: requests库,正则表达式,GUI 接下来将对代码进行整体说明 ''' import requests import re import tkinter as tk import sys from tkinter import ttk #通过观察80s网不同网页的链接可以了解到,通过如下链接去获取不同的类型https://www.y80s.net/movie/list/分类-年代-地区-语言- #根据这一点建立不同的字典,将类型与编号一一对应,当用户选择这个类型的是后就可以通过字典的键值对去获取 Language = { "国语":1 , "英语":2 , "韩语":3 , "日语":4 , "法语":5 , "其他":6 , "粤语":7 } Type = { "动作":1,"喜剧":2,"爱情":3,"科幻":4,"灾难":5,"恐怖":6,"悬疑":7,"奇幻":8,"战争":9 } Place = { "大陆":1,"香港":2,"台湾":3,"美国":4,"法国":5,"英国":6,"日本":7,"韩国":8,"德国":9 } headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36" } #获取指定页面的网页源代码 def get_txt(url): r = requests.get(url,headers=headers) r.encoding = r.apparent_encoding return r.text def set_url(): # 80s网站,不同的分类链接有一定的规律:https://www.y80s.net/movie/list/分类-年代-地区-语言- #通过字典获取不同的结果 language = Language.get(comLanguage.get()) type = Type.get(comType.get()) year = comYear.get() place = Place.get(comPlace.get()) url = "https://www.y80s.net/movie/list/"+str(type)+'-'+str(year)+'-'+str(place)+'-'+str(language)+'-' return url #利用正则表达式对得到的网页源码进行筛选,选出我们想要的内容 def select_txt(): name = [] link = [] url = set_url() s = get_txt(url) '''由于电影不同类型的堆叠,很有可能会出现找不到这一类电影的情况,比如国语-奇幻-2016-法国,满足这样四个标准的电影就没有 就需要进行判断,我们发现当没有这类电影的时候,源代码会出现一个特有的标签<div class="nomoviesinfo">,那么就可以利用这个标签''' k = re.findall('div class="nomovies(.*)">',s) #利用正则表达式去找这个特殊标签 #如果返回的数组k长度为0,说明没有这个特有标签,那么就是有满足要求的电影,就可以继续爬取 if len(k) == 0: for j in re.findall('src="//img.mimiming.com/img/grey.gif" alt="(.*)" class="p" /> ',s): name.append(j) else: for i in re.findall('<h3 class="h3"><a href="(.*?)">',s): link.append("https://www.y80s.net"+i) else: entryResult.insert(0,'对不起,没有符合该筛选条件的视频,请您放宽筛选条件') #如果返回的k长度不为0,说明有这个特有的标签,那么此时就不存在满足要求的电影 return name,link #获取了名字和符合要求的电影链接后,还需要通过不同的电影链接去获取下载该资源的链接 def down_link(): name,link = select_txt() downlink = [] #创建一个downlink数组,存放下载链接 for i in range(len(link)): s = get_txt(link[i]) i = re.findall('thunderType="" thunderPid="127891" thunderHref="(.*)">迅雷下载</A>',s) downlink.append(i) return name,downlink #将爬取的电影名和下载链接变成字典,通过txt文件保存 def save_txt(): path = entryPath.get() name,downlink = down_link() dir = dict(zip(name,downlink)) f = open(path, 'w', encoding='utf-8') for k, v in dir.items(): s2 = str(v) f.write(k + '\n') f.write(s2 + '\n') f.close() entryResult.insert(0, '保存成功') #清空,将存放地址和存放结果两栏清空 def clear(): entryPath.delete(0 , 'end') entryResult.delete(0 , 'end') #退出本程序 def exit(): sys.exit(0) rootMovie = tk.Tk() rootMovie.title("80s电影连接提取器") rootMovie['height'] = 500 rootMovie['width'] = 750 # 创建不同的标签 LabelTitle = not tk.Label(rootMovie, font = "Helvetica 22 bold italic", text="电影链接一键获取器").place(x=220,y=30) LabelType = tk.Label(rootMovie, font="GB-2312", text="语言:").place(x=100, y=100) LabelType = tk.Label(rootMovie, font="GB-2312", text="分类:").place(x=360, y=100) LabelType = tk.Label(rootMovie, font="GB-2312", text="年代:").place(x=100, y=200) LabelYear = tk.Label(rootMovie, font="GB-2312", text="地区:").place(x=360, y=200) LabelPath = tk.Label(rootMovie, font="GB-2312", text="存放地址:").place(x=100, y=280) LabelResult = tk.Label(rootMovie, font="GB-2312", text="存放结果:").place(x=100, y=330) #可供输入的文本框 entryPath = tk.Entry(rootMovie) #存放路径输入框 entryPath.place(x=200, y=280, width=400, height=30) entryResult = tk.Entry(rootMovie) #存放路径输入框 entryResult.place(x=200, y=330, width=400, height=30) #可供选择的下拉框 xVarLanguage = tk.StringVar() comLanguage = ttk.Combobox(rootMovie,textvariable=xVarLanguage) comLanguage.place(x=160, y=100, width=180, height=30) comLanguage["value"] = ("国语", "粤语", "英语", "韩语", "日语", "法语", "其他") comLanguage.current(0) xVarType = tk.StringVar() comType = ttk.Combobox(rootMovie,textvariable=xVarType) comType.place(x=420, y=100, width=180, height=30) comType["value"] = ("动作","喜剧","爱情","科幻","灾难","恐怖","悬疑","奇幻","战争") comType.current(0) xVarYear = tk.StringVar() comYear = ttk.Combobox(rootMovie,textvariable=xVarYear) comYear.place(x=160, y=200, width=180, height=30) comYear["value"] = ("2019","2018","2017","2016","2015") comYear.current(0) xVarPlace = tk.StringVar() comPlace = ttk.Combobox(rootMovie,textvariable=xVarPlace) comPlace.place(x=420, y=200, width=180, height=30) comPlace["value"] = ("大陆","香港","台湾","美国","法国","英国","日本","韩国","德国") comPlace.current(0) # 按钮 buttonGet = tk.Button(rootMovie, font="GB-2312", text="提取",command=save_txt).place(x=180, y=400, width=70, height=35) buttonClear = tk.Button(rootMovie, font="GB-2312", text="清空", command=clear).place(x=320, y=400, width=70, height=35) buttonExit = tk.Button(rootMovie, font="GB-2312", text="退出", command=exit).place(x=460, y=400, width=70, height=35) rootMovie.mainloop()
GUI界面以及结果演示
写的不好,共同学习
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- Java正则表达式获取网址和链接文字解析 .
- Python 网络爬虫 009 (编程) 通过正则表达式来获取一个网页中的所有的URL链接,并下载这些URL链接的源代码
- Python 网络爬虫 009 (编程) 通过正则表达式来获取一个网页中的所有的URL链接,并下载这些URL链接的源代码
- Java正则表达式获取网址和链接文字解析
- Python学习笔记 第二部分 - 正则表达式 与 爬虫小实例(抓取豆瓣电影中评分大于等于8分的影片)
- node.js正则表达式获取网页中所有链接的代码实例
- node.js正则表达式获取网页中所有链接的代码实例
- python爬虫之通过正则表达式获取豆瓣最新上映电影的海报
- Java正则表达式获取网址和链接文字解析
- python正则表达式re之compile函数解析
- javascript 获取链接文件地址中第一个斜线内的正则表达式
- python爬虫(实例1):requests+xpath 爬取并下载豆瓣电影的演员照片
- 正则表达式获取字符串中的img标签中的url链接
- OC利用正则表达式获取网络资源(网络爬虫)
- 爬虫笔记1 requests获取网页源代码与正则表达式处理文本
- php正则表达式获取内容所有链接
- python正则表达式实例
- 正则表达式获取字符串中的img标签中的url链接
- python3 bs4 requests 腾讯新闻 标题 链接获取 小试牛刀
- Python 正则表达式将URL转化为带链接的URL