安利动漫番太好看了!用Python爬取了1000g的动漫

927 阅读4分钟

这是我参与8月更文挑战的第16天,活动详情查看:8月更文挑战

大家好,我是白又白i。
如果你喜欢我的话就请给我点个赞呐!

image.png

最近被闺蜜安利热血动漫番《终末的女武神》和《拳愿阿修罗》,太上头了周末休息熬夜看完了。不过资源不太好找,白又白一怒爬取了资源,这下可以看个够了。闺蜜崇拜连连,想起了当年我学校的校草,就像是漫画中的男猪脚呀,阿西吧…

Python爬虫-vip动漫采集

效果展示
20210720205352636.gif

爬取目标

网站目标:樱花动漫

image.png

工具使用

开发工具:pycharm

开发环境:python3.7, Windows10

使用工具包:requests,lxml, re,tqdm

重点学习内容

正则的使用 tqdm的使用 各种音频数据的处理

项目思路解析

搜索你需要的动漫数据,根据自己需要的视频不同解析视频的方法也是不一样的(会挑选两种视频进行解析)
image.png

在当前页面需要提取出对应的章节信息,获取到章节信息的a标签的跳转内容,提取出每个章节的名字,提取章节的方法我使用的xpath的方法(各位大佬可自行尝试其他的方法)

image.png

Pythonheaders = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36',
    'Referer': 'http://www.imomoe.la/search.asp'
}
​
url = 'http://www.imomoe.la/view/8024.html'
response = requests.get(url, headers=headers)
# print(response.content.decode('gbk'))
html_data = etree.HTML(response.content.decode('gbk'))
chapter_list = html_data.xpath('//div[@class="movurl"]/ul/li/a/text()')
chapter_url_list = html_data.xpath('//div[@class="movurl"]/ul/li/a/@href')[0]

url的数据需要自行拼接,根据新的url获取详情页面的数据

image.png

按照正常思路首先应该查看播放地址是否为静态数据

image.png

明显看出数据并不是静态数据,在区分是否为动态数据,通过抓包工具进行获取。

image.png

也并不是动态数据,媒体数据也不知道怎么形成的。

image.png

从头在来从前端页面在进行解析,找视频页面的事件。

image.png

并没有发现有效数据,但是在iframe下面的Script标签有js跳转地址 ,解析的数据网址和视频的播放地址是一样的域名, 点击查看, 这不是就是我们找的视频播放地址嘛 ,终于找到了,开始实现 在当前页面通过xpath方式提取出script里的js跳转地址, 拼接出新的视频链接播放地址,发送请求,通过正则表达式提取出所有MP4播放地址。

image.png

Pythonnew_url = 'http://www.imomoe.la' + chapter_url_list
response = requests.get(new_url, headers=headers)
html = etree.HTML(response.content.decode('gbk'))
​
data_url = 'http://www.imomoe.la' + html.xpath('//div[@class="player"]/script[1]/@src')[0]
res = requests.get(data_url, headers=headers).text
# print(res)
play_url_list = re.findall('\$(.*?)\$flv', res)
print(play_url_list)

保存对视频数据发送请求,保存数据到mp4 ,通过tqdm工具能查看对应下载的速度以及下载的进度

Pythonfor chapter, play_url in tqdm(zip(chapter_list, play_url_list)):
    result = requests.get(play_url, headers=headers).content
    f = open('终末的女武神/' + chapter + '.mp4', "wb")
    f.write(result)

​到这大功告成 但是当我把网址修改成斗破苍穹这个动漫时,却返回的数据为空

ab1e649dca0a53f4acebe6d3c095ed25.png

c6cf3065217368bead4763bf7ea491d0.png

这个视频的加载数据的规则是不一样的加载的数据为m3u8的格式, 其他的音频的数据加载可能也不一样, 处理m3u8的数据稍稍的有丢丢复杂,它的m3u8的文件内部有嵌套了m3u8链接地址, 需要转换对应的数据接口,进行链接地址拼接, 取出ts文件进行下载,拼接成视频。

Pythonm3u8_url_list = re.findall('\$(.*?)\$bdhd', res)
for m3u8_url, chapter in zip(m3u8_url_list, chapter_list):
    data = requests.get(m3u8_url, headers=headers)
    # print(data.text)
    new_m3u8_url = 'https://cdn.605-zy.com/' + re.findall('/(.*?m3u8)', data.text)[0]
    # print(new_m3u8_url)
    ts_data = requests.get(new_m3u8_url, headers=headers)
    ts_url_list = re.findall('/(.*?ts)', ts_data.text)
    print("正在下载:", chapter)
    for ts_url in tqdm(ts_url_list):
        result = requests.get('https://cdn.605-zy.com/' + ts_url).content
        f = open('斗破苍穹/' + chapter + '.mp4', "ab")
        f.write(result)

项目思路总结

获取到想要动漫的地址

提取详情页面的名字已经跳转地址

获取页面的静态js文件

解析视频播放地址或者m3u8文件

保存对应数据

简易源码分享

Pythonimport requests
from lxml import etree
import re
from tqdm import tqdm
​
​
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36',
    'Referer': 'http://www.imomoe.la/search.asp'
}
​
url = 'http://www.imomoe.la/view/8024.html'
response = requests.get(url, headers=headers)
# print(response.content.decode('gbk'))
html_data = etree.HTML(response.content.decode('gbk'))
chapter_list = html_data.xpath('//div[@class="movurl"]/ul/li/a/text()')
chapter_url_list = html_data.xpath('//div[@class="movurl"]/ul/li/a/@href')[0]
# print(chapter_list)
# print(chapter_url_list)
new_url = 'http://www.imomoe.la' + chapter_url_list
response = requests.get(new_url, headers=headers)
html = etree.HTML(response.content.decode('gbk'))
​
data_url = 'http://www.imomoe.la' + html.xpath('//div[@class="player"]/script[1]/@src')[0]
res = requests.get(data_url, headers=headers).text
# print(res)
play_url_list = re.findall('\$(.*?)\$flv', res)
print(play_url_list)
​
for chapter, play_url in tqdm(zip(chapter_list, play_url_list)):
    result = requests.get(play_url, headers=headers).content
    f = open('终末的女武神/' + chapter + '.mp4', "wb")
    f.write(result)

image.png

发现不会的或者学习Python的,可以直接评论留言或者私我【非常感谢你的点赞、收藏、关注、评论,一键四连支持】