零基础教学之爬取电影的简单分析和实现(附带python代码)
新一期教学开始啦,本人也是初学,记录下爬取电影的方式,这个比较复杂了,网上的教学也比较少,希望能火吧我这里以独播库为例进行爬取1.首先需要知道,很多的电影网站会用采用m3u8视频分段的方式来加载视频,可不是咱想象中的一整个文件,分段加载的好处就是加载快,这里说到m3u8这其实是个文件,请看截图:
打开f12可以看到有两个m3u8结尾的链接,如果打开这个链接会下载m3u8文件,这里我们来下载看看这个文件的内容
很明显这个不是真正的m3u8文件,
但是里面有一条地址这个才是真正的文件地址,我们根据地址打开真实文件(这里需要拼接下,真实地址为“https://tv.wedubo.com/20191213/ny37lszg/hls/index.m3u8”),这是我们看到他的真面目
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:11
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-KEY:METHOD=AES-128,URI="https://v.zdubo.com/20191213/ny37lszg/hls/key.key"
#EXTINF:10.023,
https://v.wedubo.com/20191213/ny37lszg/hls/DMTmLuLL.ts
#EXTINF:10,
https://v.dubutv.com/20191213/ny37lszg/hls/sFnwy1KT.ts
#EXTINF:10,
https://v.duboku.tv/20191213/ny37lszg/hls/sjv8BH0a.ts
#EXTINF:10,
https://v.dboku.com/20191213/ny37lszg/hls/pOGumGn6.ts
#EXTINF:10,
https://v.zdubo.com/20191213/ny37lszg/hls/ZvyDb1nx.ts
#EXTINF:10,
https://v.tudu.site/20191213/ny37lszg/hls/Ytu2PDrQ.ts
#EXTINF:10,
https://v.fanstui.com/20191213/ny37lszg/hls/jIV5lYW3.ts
#EXTINF:10,
https://v.duboku.io/20191213/ny37lszg/hls/tlb5IswY.ts
#EXTINF:10,
https://v.wedubo.com/20191213/ny37lszg/hls/qWDCDR6W.ts
#EXTINF:10,
https://v.dubutv.com/20191213/ny37lszg/hls/QJ365WPc.ts
#EXTINF:10,
https://v.duboku.tv/20191213/ny37lszg/hls/xHuE5DwP.ts
#EXTINF:10,
https://v.dboku.com/20191213/ny37lszg/hls/Ao69Rdce.ts
#EXTINF:10,
https://v.zdubo.com/20191213/ny37lszg/hls/elqV91Wh.ts
#EXTINF:10,
https://v.tudu.site/20191213/ny37lszg/hls/tBU2DHAb.ts
#EXTINF:10,
https://v.fanstui.com/20191213/ny37lszg/hls/MjTovgaj.ts
#EXTINF:10,
https://v.duboku.io/20191213/ny37lszg/hls/PXcYDT9l.ts
#EXTINF:10,
https://v.wedubo.com/20191213/ny37lszg/hls/2xVEUnJx.ts
#EXTINF:10,
https://v.dubutv.com/20191213/ny37lszg/hls/HpeWZMXW.ts
#EXTINF:10,
https://v.duboku.tv/20191213/ny37lszg/hls/m0j0Tdmk.ts
#EXTINF:10,
https://v.dboku.com/20191213/ny37lszg/hls/Hq5FQbGK.ts
#EXTINF:10,
https://v.zdubo.com/20191213/ny37lszg/hls/ZYBQnp5l.ts
#EXTINF:10,
https://v.tudu.site/20191213/ny37lszg/hls/X2lJtbAC.ts
#EXTINF:10,
https://v.fanstui.com/20191213/ny37lszg/hls/CDFnAQK6.ts
#EXTINF:10,
https://v.duboku.io/20191213/ny37lszg/hls/7fON90jQ.ts
#EXTINF:10,
https://v.wedubo.com/20191213/ny37lszg/hls/iIxlTC5t.ts
#EXTINF:10,
https://v.dubutv.com/20191213/ny37lszg/hls/c6Z5IbES.ts
#EXTINF:10,
https://v.duboku.tv/20191213/ny37lszg/hls/tf3axbvS.ts
#EXTINF:10,
https://v.dboku.com/20191213/ny37lszg/hls/22QVEyQl.ts
#EXTINF:10,
https://v.zdubo.com/20191213/ny37lszg/hls/cNz8ARWQ.ts
#EXTINF:10,
https://v.tudu.site/20191213/ny37lszg/hls/FuACZHkp.ts
#EXTINF:10,
https://v.fanstui.com/20191213/ny37lszg/hls/tUT3mjFz.ts
#EXTINF:10,
https://v.duboku.io/20191213/ny37lszg/hls/hJmjTiXB.ts
#EXTINF:10,
https://v.wedubo.com/20191213/ny37lszg/hls/I0GY05XU.ts
#EXTINF:10,
https://v.dubutv.com/20191213/ny37lszg/hls/2mbe0MV6.ts
#EXTINF:10,
https://v.duboku.tv/20191213/ny37lszg/hls/pwylQ7PB.ts
#EXTINF:10,
https://v.dboku.com/20191213/ny37lszg/hls/pNNxAodp.ts
#EXTINF:10,
https://v.zdubo.com/20191213/ny37lszg/hls/Wy8x544A.ts
#EXTINF:10,
https://v.tudu.site/20191213/ny37lszg/hls/43jhM1hg.ts
#EXTINF:10,
https://v.fanstui.com/20191213/ny37lszg/hls/R5VkhPDI.ts
#EXTINF:10,
https://v.duboku.io/20191213/ny37lszg/hls/HIOZVw44.ts
#EXTINF:10,
https://v.wedubo.com/20191213/ny37lszg/hls/kkVAYMX1.ts
#EXTINF:10,
https://v.dubutv.com/20191213/ny37lszg/hls/ndRU43WQ.ts
#EXTINF:10,
https://v.duboku.tv/20191213/ny37lszg/hls/c3CoaA3n.ts
#EXTINF:10,
https://v.dboku.com/20191213/ny37lszg/hls/MSMJIZnY.ts
#EXTINF:10,
https://v.zdubo.com/20191213/ny37lszg/hls/lemaTMuh.ts
#EXTINF:10,
https://v.tudu.site/20191213/ny37lszg/hls/59DkJ7LT.ts
#EXTINF:10,
https://v.fanstui.com/20191213/ny37lszg/hls/1mDH02Pg.ts
#EXTINF:10,
https://v.duboku.io/20191213/ny37lszg/hls/KXThI3yN.ts
#EXTINF:10,
https://v.wedubo.com/20191213/ny37lszg/hls/ZNj9MQFE.ts
#EXTINF:10,
https://v.dubutv.com/20191213/ny37lszg/hls/w24cxqRr.ts
#EXTINF:10,
https://v.duboku.tv/20191213/ny37lszg/hls/Ccf8nqlL.ts
#EXTINF:10,
https://v.dboku.com/20191213/ny37lszg/hls/ymB41F7s.ts
#EXTINF:10,
https://v.zdubo.com/20191213/ny37lszg/hls/a9elfGcb.ts
#EXTINF:10,
https://v.tudu.site/20191213/ny37lszg/hls/YFDFGIbh.ts
#EXTINF:10,
https://v.fanstui.com/20191213/ny37lszg/hls/ZNIwu9wM.ts
#EXTINF:10,
https://v.duboku.io/20191213/ny37lszg/hls/WokCsZVT.ts
#EXTINF:10,
https://v.wedubo.com/20191213/ny37lszg/hls/ZVMTGHpw.ts
#EXTINF:10,
https://v.dubutv.com/20191213/ny37lszg/hls/IZC6y0AX.ts
#EXTINF:10,
https://v.duboku.tv/20191213/ny37lszg/hls/drcTkcfL.ts
#EXTINF:10,
https://v.dboku.com/20191213/ny37lszg/hls/At68E4Lv.ts
#EXTINF:9.6,
https://v.zdubo.com/20191213/ny37lszg/hls/3VDDxZm8.ts
#EXT-X-ENDLIST
里面有很多的ts结尾的地址,这里的ts其实就是一个个视频,不过都是片段,把这些ts合并后才是真正的我们要的电影
2.然后我们获取到了ts文件地址,所以你以为结束了吗,不,如果你下载了其中一个ts文件你会发现,根本打不开,这是因为视频进行了加密,这时我们注意到在m3u8文件中有这么一句代码
#EXT-X-KEY:METHOD=AES-128,URI="https://v.zdubo.com/20191213/ny37lszg/hls/key.key"
其中的AES-128是加密方式,其中的key是。。。,就是一个key,可以理解为钥匙,我们下载下这个文件
4b56f3209ca9bbef
我们发现里面只有个这玩意,所以我们钥匙有了,锁也有了,到这基本可以去想想代码该怎么实现了
钥匙都给你了,锁也给你了,所以该用钥匙打开锁就要靠你们百度了,这里只讲解怎么用python实现,其他语言咱也么写过
import os
import requests
import re
from Crypto.Cipher import AES
from bs4 import BeautifulSoup
"""
下载M3U8文件里的所有片段
"""
def combine_mp4(path):
# 合并ts文件
os.chdir(path)
shell_str = '+'.join(os.listdir(path))
shell_str = 'copy /b ' + shell_str + ' ' + '1.mp4'
os.system(shell_str)
# 删除ts文件
os.system('del /Q *.ts')
def download(url):
# os.getcwd() 获取当前文件路径
download_path = os.getcwd() + "\download"
# 不存在文件夹创建
if not os.path.exists(download_path):
os.mkdir(download_path)
all_content = requests.get(url).text # 获取M3U8的文件内容
# print(all_content)
file_line = all_content.split("\n") # 读取文件里的每一行
# 通过判断文件头来确定是否是M3U8文件
if file_line != "#EXTM3U":
raise BaseException(u"非M3U8的链接")
else:
unknow = True # 用来判断是否找到了下载的地址
cryptor = None # 用于AES的解密
for index, line in enumerate(file_line):
if "EXT-X-KEY" in line:
str1 = str(file_line)
url = re.findall('"(.*?)"', str1)
print(url)
key = requests.get(str(url)).content
cryptor = AES.new(key, AES.MODE_CBC, key)# key和解密方式,AES我不太熟,请自行百度
if "EXTINF" in line:
unknow = False
pd_url = file_line# ts文件的路径
res = requests.get(pd_url)
s = str(file_line)
c_fule_name = s
print(c_fule_name+'正在下载')
with open(download_path + "\\" + c_fule_name, 'ab') as f:
try:
f.write(cryptor.decrypt(res.content))
f.flush()
f.close()
except:
continue
res.close()
if unknow:
raise BaseException("未找到对应的下载链接")
else:
print("下载完成")
combine_mp4(download_path)
# 获取页面的m3u8文件地址
def get_m3u8(url):
res = BeautifulSoup(requests.get(url).content, 'lxml')
# 获取第九个script标签的内容,地址就在其中
str1 = str(res.find_all("script"))
# 根据引号分割
url = re.findall('"(.*?)"', str1)
# 这里是知道url在第13个,所以就不加循环去判断了,循环多了影响效率
replace = str(url).replace("/", "").replace("\\", '/')
# 到这里虽然获取到了m3u8文件,但是不是真正的的文件,这个假的中有真的的存放位置
text = requests.get(replace).text
file_line = text.split("\n")# 读取文件里的每一行
# url_str为https://v.tudu.site,因为m3u8文件中的地址不全,所以需要拼接下
url_str = re.findall('https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+', replace)
for line in file_line:
if 'm3u8' in line:
return url_str+line
if __name__ == '__main__':
download(get_m3u8("https://cn.duboku.tv/vodplay/1354-1-4.html"))
这里可能是因为网站或者网速的原因,我下载时很慢,大家还是把我的代码当做学习来用吧,实用性不强 如果缺AES的话,可以先运行这个
pip install PyCryptodome Albert666 发表于 2020-9-9 15:45
这里是因为这个网站key都直接在了有些网站是没有这么明显的key在的吧。很多都是加密的 连m3u8文件都加密
所以盗版网站就很好爬 这里是因为这个网站key都直接在了有些网站是没有这么明显的key在的吧。很多都是加密的 连m3u8文件都加密 来看看 学习一下 {:1_901:}嗯,, 看来还得研究一下python环境···· 不明觉厉 python还不会,打算学不知道怎么学好,不知道是否好学。 看来派森=爬虫
大家都用来爬 学习一下 学习了,
页:
[1]
2