分析主要任务:
一键获取 80S手机电影网(https://www.y80s.net/)不同年代的电影下载链接,可以根据用户选择的不同年代比如XX年的XX类型电影来提取前4页(x个)不同电影的下载地址并存放到指定的txt文本(或者excel文件中)
主要界面设计:
年代选择:是几几年的电影,可供用户去选择——首先直接输入——下拉框
什么类型的电影:动作、悬疑、搞笑等等不同类型——首先直接输入——下拉框
最终链接保存的地点:保存地址的输入
函数设计:
- 获取指定网页的源代码并通过字符串的形式返回,
get_txt(url)
- 筛选函数,从网页源代码获取电影名称和链接,
select _txt()
,名字和链接分别放到两个数组里面,并返回这两个数组 - 通过链接数组进一步获取电影的下载地址,
down_link()
,将电影的下载地址作为一个数组返回 - 将包含电影名字的数组和下载地址的数组一一对应成为一个字典,保存到指定的地方
import requests
import re
import tkinter as tk
import sys
#获取指定页面的源代码
def get_txt(url):
r = requests.get(url)
r.encoding = r.apparent_encoding
return r.text
def select_txt():
name = []
link = []
s = get_txt("https://www.y80s.net/movie/list/-2019----p")
for j in re.findall('src="//img.mimiming.com/img/grey.gif" alt="(.*)" class="p" /> ',s):
name.append(j)
for i in re.findall('<h3 class="h3"><a href="(.*?)">',s):
link.append("https://www.y80s.net"+i)
return name,link
def down_link():
name,link = select_txt()
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
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()
def clear():
entryType.delete(0, 'end')
entryYear.delete(0, 'end')
entryPath.delete(0, 'end')
def exit():
sys.exit(0)
rootMovie = tk.Tk()
rootMovie.title("80s电影连接提取器")
rootMovie['height'] = 500
rootMovie['width'] = 750
# 标签
LabelType = tk.Label(rootMovie, font="GB-2312", text="电影类别:").place(x=100, y=100)
LabelYear = tk.Label(rootMovie, font="GB-2312", text="年份:").place(x=100, y=200)
LabelPath = tk.Label(rootMovie, font="GB-2312", text="存放地址:").place(x=100, y=300)
# 输入文本框
entryType = tk.Entry(rootMovie)
entryType.place(x=200, y=100, width=400, height=30)
entryYear = tk.Entry(rootMovie)
entryYear.place(x=200, y=200, width=400, height=30)
entryPath = tk.Entry(rootMovie)
entryPath.place(x=200, y=300, width=400, height=30)
# 按钮
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()
结果如下:
声明目前本代码只能爬取一个链接的25个电影下载链接,还包含很多的不足,之后会进一步修改,获取更多的功能,爬取更多的链接,感谢阅读!