首页 > 其他分享 >【爬虫实例2】91网视频爬取

【爬虫实例2】91网视频爬取

时间:2022-08-14 14:44:16浏览次数:57  
标签:m3u8 url resp 爬虫 爬取 video 91 line requests

1、 导入模块

import requests
import re

2、获取m3u8文件

# url地址
url = 'http://www.wwmulu.com/rj/xhcl/play-1-1.html'
# 正则表达式
obj = re.compile(r'<span class="ff-player" data-play-name="kbm3u8" data-src="(?P<video_url>.*?)"',re.S)
# 获取网页
resp = requests.get(url)
# 网页编码处理
resp.encoding = 'ISO-8859-1'
# 正则匹配得到需要的m3u8地址
m3u8 = obj.search(resp.text).group("video_url")
# m3u8内容获取
resp2 = requests.get(m3u8)
with open("video.m3u8",mode='wb') as f:
    f.write(resp2.content)
resp2.close()

在这里插入图片描述

3、下载切片后的视频

n = 1
with open("video.m3u8",mode="r",encoding="utf-8") as f:
   for line in f:
        line = line.strip()
        print(line)
        # 提取视频切片地址
        if line.startswith("#"):
            continue
        # 获取视频内容
        rep3 = requests.get(line)
        f = open(f"./video/{n}.ts", mode="wb")
        f.write(rep3.content)
        f.close()
        rep3.close()
        n += 1
        print(f"{n}succ")

4、完整代码

import requests
import re

# url地址
url = 'http://www.wwmulu.com/rj/xhcl/play-1-1.html'
# 正则表达式
obj = re.compile(r'<span class="ff-player" data-play-name="kbm3u8" data-src="(?P<video_url>.*?)"', re.S)
# 获取网页
resp = requests.get(url)
# 网页编码处理
resp.encoding = 'ISO-8859-1'
# 正则匹配得到需要的m3u8地址
m3u8 = obj.search(resp.text).group("video_url")
# m3u8内容获取
resp2 = requests.get(m3u8)
with open("video.m3u8", mode='wb') as f:
    f.write(resp2.content)
resp2.close()
n = 1
with open("video.m3u8",mode="r",encoding="utf-8") as f:
   for line in f:
        line = line.strip()
        print(line)
        # 提取视频切片地址
        if line.startswith("#"):
            continue
        # 获取视频内容
        rep3 = requests.get(line)
        f = open(f"./video/{n}.ts", mode="wb")
        f.write(rep3.content)
        f.close()
        rep3.close()
        n += 1
        print(f"{n}succ")

5、 展示

在这里插入图片描述

6、视频合并

在这里插入图片描述

标签:m3u8,url,resp,爬虫,爬取,video,91,line,requests
From: https://www.cnblogs.com/nnguhx/p/16585411.html

相关文章

  • AcWing3391.今年第几天?(日期题)
    原题链接https://www.acwing.com/problem/content/3394/日期题思路满足下面条件之一的是闰年:年份是4的整数倍,而且不是100的整数倍;年份是400的整数倍。处理输......
  • 爬虫数据分析-Xpath
    1.环境安装:-pipinstalllxml2.如何实例化一个etree对象:fromlxmlimportetree(1)将本地的html文档中的源码数据加载到etree对象中:etree.parse(filePath)(2)可......