rest api 做网站,做虚假彩票网站判几年,wordpress 和wiki,深圳企业网站制作推广运营Scrapy 爬取m3u8视频
【一】效果展示
爬取ts文件样式 合成的MP4文件 【二】分析m3u8文件路径
视频地址#xff1a;[在线播放我独自升级 第03集 - 高清资源](https://www.physkan.com/ph/175552-8-3.html)
【1】找到m3u8文件
这里任务目标很明确 就是找m3u8文件 打开浏览器…Scrapy 爬取m3u8视频
【一】效果展示
爬取ts文件样式 合成的MP4文件 【二】分析m3u8文件路径
视频地址[在线播放我独自升级 第03集 - 高清资源](https://www.physkan.com/ph/175552-8-3.html)
【1】找到m3u8文件
这里任务目标很明确 就是找m3u8文件 打开浏览器 进入开发者模式F12搜索m3u8文件查看响应内容含有ts文件的m3u8文件再次查看标头地址即可 【2】分析m3u8路径
https://leshiyuncdn.36s.top/20240121/0RS6t7a1/2000kb/hls/index.m3u8 按照/拆分leshiyuncdn.36s.top----20240121----0RS6t7a1----2000kb----hls笨办法一个个的进行搜索查看哪个找到m3u8的路径 其中搜索leshiyuncdn.36s.top这个的时候 查看响应中含有m3u8地址那么就继续分析这个地址 https://bfnb1sx.phvod.top/?urlO0O0OlHnRp0hcpHM6Ly9sZXNoO0O0OXl1bmNkbi4zNnMuo000oG9wLzIwMjQwMTIxLzBSUzZ0N2ExL2luZGV4Lm0zo000oTgoo00onext//www.physkan.com/ph/175552-8-4.html 同样的采用笨方法拆分一个一个的找 在搜索O0O0OlHnRp0hcpHM6Ly9sZXNoO0O0OXl1bmNkbi4zNnMuo000oG9wLzIwMjQwMTIxLzBSUzZ0N2ExL2luZGV4Lm0zo000oTgoo00o的时候 找到https://www.physkan.com/ph/175552-8-3.html里面含有我们搜索的内容并且这个地址就是浏览器的访问视频的地址好了就是它了 【三】scrapy代码
【1】基础内容
class M3U8Spider(scrapy.Spider):# 爬虫文件名name m3u8# 可访问的域名列表allowed_domains [www.physkan.com, bfnb1sx.phvod.top, leshiyuncdn.36s.top, tscdn.hyz1.top]# 起始地址start_urls [https://www.physkan.com/ph/175552-8-3.html]# 视频存储路径video_path os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), video)# 确保文件创建好os.makedirs(video_path, exist_okTrue)# m3u8文件路径m3u8_path os.path.join(video_path, index.m3u8)# ts文件路径ts_info_path os.path.join(video_path, ts.txt)【2】分析获取m3u8路径
我们需要的数据发现在script的player_aaaa中 正则匹配json格式转换为字典格式方便读取数据其中url含有我们需要的路径参数但是不全所以补全路径发起请求
def parse(self, response):# 获取网页源码page_source response.text# 分析源码可以发现需要的地址在script的player_aaaa中# 通过正则匹配获取pattern rvar player_aaaa({.*?})/scripturl_info_str re.findall(pattern, page_source, re.DOTALL)[0]# json格式转换为字典方便拿数据url_info_dict json.loads(url_info_str)# 拼接m3u8路径m3u8_info_url https://bfnb1sx.phvod.top/?url url_info_dict[url]yield scrapy.Request(urlm3u8_info_url, callbackself.get_m3u8_url)这个地址还并非是直接的m3u8路径 同样的获取m3u8路径参数拼接完整路径参数就可以得到m3u8的真正路径
def get_m3u8_url(self, response):page_source response.textpattern rvar config ({.*?})m3u8_info_str re.findall(pattern, page_source, re.DOTALL)[0]m3u8_info_dict json.loads(m3u8_info_str)m3u8_url m3u8_info_dict[url]m3u8_url m3u8_url.rsplit(/, 1)[0] /2000kb/hls/index.m3u8yield scrapy.Request(urlm3u8_url, callbackself.get_ts_list)【3】获取过滤ts
通过上面的地址获取到了index.m3u8文件 先保存在本地一份方便查看使用正则表达式过滤出ts视频还要保存一份ts文件路径在本地 因为接下来使用ffmpeg工具进行视频合成格式要求file 视频路径.ts 最后异步发起ts视频文件请求
def get_ts_list(self, response):# 获取页面txt信息page_source response.text# 保存在index.m3u8文件在本地with open(self.m3u8_path, modewt, encodingutf8) as fp:fp.write(page_source)# 使用正则过滤拿出ts路径ts_urls re.findall(rhttps://tscdn.hyz1.top/[^\s].ts, page_source)# 保存的ts视频文件需要按照合成视频ffmpeg的格式拼接with open(self.ts_info_path, modewt, encodingutf8) as fp:for ts in ts_urls:file_name ts.rsplit(/, 1)[-1]file_path os.path.join(self.video_path, file_name)# 保存ts文件保存的为ts文件路径fp.write(ffile {file_path} \n)# 异步发起ts视频文件的请求yield scrapy.Request(urlts, callbackself.save_ts_file, meta{file_path: file_path})3.1小插曲
在m3u8文件中 你会发现这个不一样的地址其实这部分是广告可以过滤掉 【4】保存ts文件、合成MP4文件
首先进行ts文件保存 这个没有什么好说的直接保存吧
def save_ts_file(self, response):# 保存ts文件本地file_path response.meta.get(file_path)with open(file_path, modewb) as fp:fp.write(response.body)# 输出日志写不写都行self.log(f保存成功:{file_path.rsplit(/, 1)[-1]})拼接ts文件为MP4视频文件 需要用的工具是ffmpeg 官网Download FFmpeg 去安装配置好环境变量即可 合成MP4视频 首先使用os模块切换到保存的ts文件路径下然后执行ffmpeg命令 ffmpeg -f concat -safe 0 -i ts.txt -c copy output.mp4 ts.txt是之前的保存的ts文件路径文件 格式要求file 视频路径.ts output.mp4是合成后的mp4文件 可自定义文件名等 def close(spider, reason):# 爬虫执行完毕以后拼接视频 工具ffmpegos.chdir(f{spider.video_path})os.system(fffmpeg -f concat -safe 0 -i ts.txt -c copy output.mp4)免责声明 本爬虫仅用于收集特定网站的信息目的是进行数据分析不得用于非法目的或侵犯他人隐私。对于因使用本爬虫造成的任何损失或法律责任本人概不负责。 本爬虫的数据可能存在不准确、不完整或不可用的情况对于用户或第三方可能因此造成的任何损失本人概不负责。