上一篇中我们写了一半了,本文在上一篇的基础上继续进行。
1、特殊处理点
需要注意一个问题,之前我们取值的时候,都是一行一行读取的,然后从中获取关键字,那么如果是下面的情况呢?

可以看到一个电影里面往往有很多个演员,这种时候我们如果再根据关键字演员来筛选的话,肯定是不合适的,那么我们采取下面的方式来进行;
if info.startswith('◎演 员'):
Video_Yanyuan = info.replace('◎演 员', '')
print('Video_Yanyuan:' + Video_Yanyuan)
for x in range(index + 1, len(Detail_Content)):
actor = Detail_Content[x]
if actor.startswith("◎"):
break
print(actor)
上面的意思是说,当我们获取到◎演 员关键字之后,继续往下按行轮训获取值,直到下一个关键字◎出现为止,这样子就获取到了所有的演员名单。
2、初步代码
当我们所有的问题点解决完之后,我们此时的代码应该如下:
# 导入模块
import requests
from lxml import etree
# 请求头
Headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',
'Referer': 'https://www.ygdy8.net/html/gndy/dyzz/index.html'
}
Base_Url = 'https://www.ygdy8.net'
# 请求第一页
One_Url = 'https://www.ygdy8.net/html/gndy/dyzz/list_23_1.html'
Requ = requests.get(url=One_Url, headers=Headers)
Text = Requ.content.decode('gbk')
# print(Text)
# 获取中间部分视频的url
Html = etree.HTML(Text)
One_Page = Html.xpath("//div[@class='co_content8']//a")
# print(One_Page)
# 获取每个url,并轮训请求
for Details_Url in One_Page:
Href = Details_Url.xpath("@href")[0]
# print(Href)
# 找到以“/”开头的
if Href.startswith('/'):
Detail_Url = Base_Url + Href
One_Detail = requests.get(url=Detail_Url, headers=Headers)
One_Detail_Html = One_Detail.content.decode('gbk')
# print(One_Detail.content.decode('gbk'))
Detail_Html = etree.HTML(One_Detail_Html)
Detail_Content = Detail_Html.xpath("//div[@id='Zoom']//text()")
# print(Detail_Content)
for index, info in enumerate(Detail_Content):
# 先写三个示例
if info.startswith('◎译 名'):
Video_Name_CN = info.replace('◎译 名', '')
print('Video_Name_CN:' + Video_Name_CN)
if info.startswith('◎片 名'):
Video_Name = info.replace('◎片 名', '')
print('Video_Name:' + Video_Name)
if info.startswith('◎产 地'):
Video_Address = info.replace('◎产 地', '')
print('Video_Address' + Video_Address)
if info.startswith('◎类 别'):
Video_Type = info.replace('◎类 别', '')
print('Video_Type: ' + Video_Type)
if info.startswith('◎语 言'):
Video_language = info.replace('◎语 言', '')
print('Video_Language:' + Video_language)
if info.startswith('◎上映日期'):
Video_Date = info.replace('◎上映日期', '')
print("Video_Date:" + Video_Date)
if info.startswith('◎豆瓣评分'):
Video_Number = info.replace('◎豆瓣评分', '')
print("Video_Number:" + Video_Number)
if info.startswith('◎片 长'):
Video_Time = info.replace('◎片 长', '')
print('Video_Time:' + Video_Time)
if info.startswith('◎导 演'):
Video_Daoyan = info.replace('◎导 演', '')
print('Video_Daoyan:' + Video_Daoyan)
if info.startswith('◎演 员'):
Video_Yanyuan = info.replace('◎演 员', '')
print('Video_Yanyuan:' + Video_Yanyuan)
for x in range(index + 1, len(Detail_Content)):
actor = Detail_Content[x]
if actor.startswith("◎"):
break
print(actor)
# 只允许请求一次
break
此时我们的运行结果如下:
Video_Name_CN: 正发生
Video_Name:L'Événement / Happening
Video_Address 法国
Video_Type: 剧情
Video_Language: 法语
Video_Date:2021-09-06(威尼斯电影节) / 2021-11-24(法国)
Video_Number:7.8/10 from 7333 users
Video_Time:100分钟
Video_Daoyan: 奥黛丽·迪万 Audrey Diwan
Video_Yanyuan: 安娜玛丽亚·沃特鲁梅 Anamaria Vartolomei
卡西·莫泰·克莱恩 Kacey Mottet Klein
卢安娜·巴杰拉米 Luàna Bajrami
露易丝·奥利-狄奎罗 Louise Orry-Diquéro
路易丝·舍维约特 Louise Chevillotte
皮奥·马麦 Pio Marmaï
桑德里娜·博内尔 Sandrine Bonnaire
安娜·穆格拉利斯 Anna Mouglalis
阿丽斯·德·朗克桑 Alice de Lencquesaing
法布里齐奥·隆吉奥内 Fabrizio Rongione
塞德里克·梅斯伯格 Cédric Meusburger
3、完善代码
当我们能获取到第一页的第一个视频的相关信息之后,我们是不是进行多次轮训就可以了;
但是还有另外一个问题,一份优雅的代码不应该是一系列代码的堆叠,我们需要进行相关的优化,我们这里就将代码拆分成几个文件吧。
4、完整代码
import requests
from lxml import etree
# 请求头
Headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',
'Referer': 'https://www.ygdy8.net/html/gndy/dyzz/index.html'
}
# 视频详情页Url拼接地址
Base_Url = 'https://www.ygdy8.net'
def Get_Detail(Details_Url):
Href = Details_Url.xpath("@href")[0]
if Href.startswith('/'):
Video_Message = {}
Detail_Url = Base_Url + Href
One_Detail = requests.get(url=Detail_Url, headers=Headers)
One_Detail_Html = One_Detail.content.decode('gbk')
Detail_Html = etree.HTML(One_Detail_Html)
Detail_Content = Detail_Html.xpath("//div[@id='Zoom']//text()")
for index, info in enumerate(Detail_Content):
if info.startswith('◎译 名'):
Video_Name_CN = info.replace('◎译 名', '').strip()
Video_Message['Video_Name_CN'] = Video_Name_CN
if info.startswith('◎片 名'):
Video_Name = info.replace('◎片 名', '').strip()
Video_Message['Video_Name'] = Video_Name
if info.startswith('◎产 地'):
Video_Address = info.replace('◎产 地', '').strip()
Video_Message['Video_Address'] = Video_Address
if info.startswith('◎类 别'):
Video_Type = info.replace('◎类 别', '').strip()
Video_Message['Video_Type'] = Video_Type
if info.startswith('◎语 言'):
Video_language = info.replace('◎语 言', '').strip()
Video_Message['Video_language'] = Video_language
if info.startswith('◎上映日期'):
Video_Date = info.replace('◎上映日期', '').strip()
Video_Message['Video_Date'] = Video_Date
if info.startswith('◎豆瓣评分'):
Video_Number = info.replace('◎豆瓣评分', '').strip()
Video_Message['Video_Number'] = Video_Number
if info.startswith('◎片 长'):
Video_Time = info.replace('◎片 长', '').strip()
Video_Message['Video_Time'] = Video_Time
if info.startswith('◎导 演'):
Video_Daoyan = info.replace('◎导 演', '').strip()
Video_Message['Video_Daoyan'] = Video_Daoyan
if info.startswith('◎演 员'):
Video_Yanyuan_list = []
Video_Yanyuan = info.replace('◎演 员', '').strip()
Video_Yanyuan_list.append(Video_Yanyuan)
for x in range(index + 1, len(Detail_Content)):
actor = Detail_Content[x].strip()
if actor.startswith("◎"):
break
Video_Yanyuan_list.append(actor)
Video_Message['Video_Yanyuan_list'] = Video_Yanyuan_list
return Video_Message
def Get_Video(Pages):
for Page in range(1, Pages + 1):
Page_Url = 'https://www.ygdy8.net/html/gndy/dyzz/list_23_{}.html'.format(Page)
Requ = requests.get(url=Page_Url, headers=Headers)
Text = Requ.text
Html = etree.HTML(Text)
One_Page = Html.xpath("//div[@class='co_content8']//a")
for Details_Url in One_Page:
jier = Get_Detail(Details_Url)
print(jier)
if __name__ == '__main__':
Get_Video(5)
然后我们运行之后的输出结果是如下样式的,仅列出部分:
{'Video_Name_CN': '正发生', 'Video_Name': "L'Événement / Happening", 'Video_Address': '法国', 'Video_Type': '剧情', 'Video_language': '法语', 'Video_Date': '2021-09-06(威尼斯电影节) / 2021-11-24(法国)', 'Video_Number': '7.8/10 from 7333 users', 'Video_Time': '100分钟', 'Video_Daoyan': '奥黛丽·迪万 Audrey Diwan', 'Video_Yanyuan_list': ['安娜玛丽亚·沃特鲁梅 Anamaria Vartolomei', '卡西·莫泰·克莱恩 Kacey Mottet Klein', '卢安娜·巴杰拉米 Luàna Bajrami', '露易丝·奥利-狄奎罗 Louise Orry-Diquéro', '路易丝·舍维约特 Louise Chevillotte', '皮奥·马麦 Pio Marmaï', '桑德里娜·博内尔 Sandrine Bonnaire', '安娜·穆格拉利斯 Anna Mouglalis', '阿丽斯·德·朗克桑 Alice de Lencquesaing', '法布里齐奥·隆吉奥内 Fabrizio Rongione', '塞德里克·梅斯伯格 Cédric Meusburger']}
{'Video_Name_CN': '沙丘/沙丘瀚战(港)', 'Video_Name': 'Dune/Dune: Part One', 'Video_Address': '美国', 'Video_Type': '剧情/科幻/冒险', 'Video_language': '国英双语', 'Video_Date': '2021-09-03(威尼斯电影节) / 2021-09-11(多伦多电影节) / 2021-10-22(美国/中国大陆)', 'Video_Number': '7.8/10 from 415165 users', 'Video_Time': '156分钟', 'Video_Daoyan': '丹尼斯·维伦纽瓦 Denis Villeneuve'}
{'Video_Name_CN': '温德米尔儿童', 'Video_Name': 'The Windermere Children / The Children', 'Video_Address': '英国', 'Video_Type': '剧情/战争', 'Video_language': '英语', 'Video_Date': '2020-01-27(英国)', 'Video_Number': '7.4/10 from 1207 users', 'Video_Time': '88分钟', 'Video_Daoyan': '迈克尔·塞缪尔斯 Michael Samuels', 'Video_Yanyuan_list': ['托马斯·克莱舒曼 Thomas Kretschmann', '萝玛拉·嘉瑞 Romola Garai', '伊恩·格雷 Iain Glen', '蒂姆·麦克纳尼 Tim McInnerny', '马塞尔·萨巴特 Marcel Sabat', '菲利普·克里斯托弗 Philipp Christopher', '安娜·舒马赫 Anna Schumacher', '爱丽斯·怀特 Alice White', 'B·J·\xa0\xa0霍格 B.J. Hogg']}
{'Video_Name_CN': '心烦意乱/爱之彩', 'Video_Name': 'Crush/Love in Color', 'Video_Address': '美国', 'Video_Type': '喜剧/爱情', 'Video_language': '英语', 'Video_Date': '2022-04-29(美国网络)', 'Video_Number': '7.3/10 from 947 users', 'Video_Time': '93分钟'}
{'Video_Name_CN': '坏蛋联盟/坏家伙/大坏蛋', 'Video_Name': 'The Bad Guys/Bad Guys', 'Video_Address': '美国', 'Video_Type': '喜剧/动画/冒险', 'Video_language': '英语', 'Video_Date': '2022-03-17(阿根廷) / 2022-04-22(美国) / 2022-04-29(中国大陆)', 'Video_Number': '7.5/10 from 20770 users', 'Video_Time': '100分钟', 'Video_Daoyan': '彼埃尔·佩里菲尔 Pierre Perifel'}
{'Video_Name_CN': '茜色如烧/茜色照映', 'Video_Name': '茜色に焼かれる/A Madder Red/Burned in Akane', 'Video_Address': '日本', 'Video_Type': '剧情', 'Video_language': '日语', 'Video_Date': '2021-05-21(日本)', 'Video_Number': '7.1/10 from 625 users', 'Video_Time': '144分钟', 'Video_Daoyan': '石井裕也 Yûya Ishii'}
{'Video_Name_CN': '泡泡', 'Video_Name': 'バブル/Bubble', 'Video_Address': '日本', 'Video_Type': '动画', 'Video_language': '国日双语', 'Video_Date': '2022-02-14(柏林电影节) / 2022-04-28(日本网络) / 2022-05-13(日本)', 'Video_Number': '5.8/10 from 2474 users', 'Video_Time': '100分钟', 'Video_Daoyan': '荒木哲郎 Tetsurō Araki'}
{'Video_Name_CN': 'Only Fools Rush In', 'Video_Name': '四海/年轻的故事', 'Video_Address': '中国大陆', 'Video_Type': '喜剧/动作/爱情', 'Video_language': '普通话', 'Video_Date': '2022-02-01(中国大陆)', 'Video_Number': '5.5/10 from 147668 users', 'Video_Time': '128分钟', 'Video_Daoyan': '韩寒 Han Han'}
{'Video_Name_CN': 'The Battle At Lake Changjin II/Watergate Bridge', 'Video_Name': '长津湖之水门桥/三炸水门桥/水门桥/长津湖(下)/长津湖2', 'Video_Address': '中国大陆,中国香港', 'Video_Type': '剧情/历史/战争', 'Video_language': '普通话', 'Video_Date': '2022-02-01(中国大陆)', 'Video_Number': '7.2/10 from 243928 users', 'Video_Time': '149分钟', 'Video_Daoyan': '徐克 Hark Tsui'}
{'Video_Name_CN': '致那些得不到保护的人', 'Video_Name': '護られなかった者たちへ/What Happened to Our Nest Egg!?', 'Video_Address': '日本', 'Video_Type': '剧情/犯罪', 'Video_language': '日语', 'Video_Date': '2021-10-01(日本)', 'Video_Number': '6.8/10 from 1017 users', 'Video_Time': '134分钟', 'Video_Daoyan': '濑濑敬久 Takahisa Zeze'}
{'Video_Name_CN': '逃亡/无处安心(港)/逃不出的记忆', 'Video_Name': 'Flugt/Flee', 'Video_Address': '丹麦,瑞典,挪威,法国,美国,斯洛文尼亚,爱沙尼亚,西班牙,意大利,芬兰', 'Video_Type': '动画/纪录片', 'Video_language': '丹麦语,英语,俄语,瑞典语', 'Video_Date': '2021-01-28(圣丹斯电影节) / 2021-06-17(丹麦)', 'Video_Number': '8.2/10 from 7430 users', 'Video_Time': '88分钟', 'Video_Daoyan': '乔纳斯·波赫·拉斯穆森 Jonas Poher Rasmussen'}
后面等学习了数据存储之类的知识之后,我们可以将其结果保存在数据库或者excel
中,进行持久化保存了。
至此 ,本文结束,相关内容每日更新。
文章转载自运维家,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




