Use Python to collect short video content from a website and download m3u8 video content

Preface

Hi~ Hello everyone, I’m Xiaoman❤~!
Insert image description here

Environmental use:

  • Python 3.8 to run the code
  • pycharm 2021.2 assists in typing code

The module uses:

  • import requests >>> pip install requests

For built-in modules, you just need to install the python environment.

  • import re
  • import json

Code implementation steps

1. Send a request, simulate the browser sending a request to the url address

Request link: <Video play page>

2. Get the data and get the response data returned by the server

Developer Tools: response

3. Parse the data and extract the data content we need

  • m3u8 file link
  • video title

4. Send a request and simulate the browser sending a request to the url address

Request link: m3u8 file link

5. Get the data and get the response data returned by the server

Developer tools: response <m3u8 return data>

6. Parse the data and extract the data content we need

All ts video clip links

7. Save the data, get all the video clips, and integrate them into a complete video content
Insert image description here

Code display

# 导入数据请求模块
import requests
# 导入正则表达式
import re
# 导入json模块
import json
# 导入格式化输出模块
from pprint import pprint
# 模拟浏览器 请求头, 字典数据 <根据网站可以选择添加某些参数>
headers = {
    
    
    # 用户代理 表示浏览器基本身份信息
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'
}
link = 'https://****/u/29946310?quickViewId=ac-space-video-list&reqID=3&ajaxpipe=1&type=video&order=newest&page=2&pageSize=20&t=1689859405043'
link_data = requests.get(url=link, headers=headers).text
video_id_list = re.findall('"atomid.*?":.*?"(\d+).*?",', link_data)
for video_id in video_id_list:
    # 请求链接
    url = f'https://****/v/ac{video_id}'
    # 发送请求
    response = requests.get(url=url, headers=headers)
    # 视频标题
    title = re.findall('"title":"(.*?)",', response.text)[1]
    print(title)
    # 提取视频信息 <m3u8>
    html_data = re.findall('window.pageInfo = window.videoInfo = (.*?);', response.text)[0]
    json_data = json.loads(html_data)
    m3u8_url = json.loads(json_data['currentVideoInfo']['ksPlayJson'])['adaptationSet'][0]['representation'][0]['backupUrl'][0]
    m3u8_data = requests.get(url=m3u8_url, headers=headers).text
    # 替换 <删除>
    ts_info = re.sub('#E.*', '', m3u8_data).split()
    # for循环
    page = 1
    for ts in ts_info:
        # https://ali-safety-video.acfun.cn/mediacloud/acfun/acfun_video/3312e674b5c67510-0246f3db30d62240df35dfbd9db8fa1d-hls_720p_hevc_1.00000.ts?pkey=ABAWBr0wZN0700m3-3VDmVvb7FvV2zLnb14EBXZyRHSRddtlEBs9Z-DUrG9j26dmhfmBEs9kKtBWg4mx5TlnFdinobquiqeov-oH78KRaMcTgs3q5JYYMGNczJZ_iqalak9u1mZ722SqzCrfgZKMUaqLxKyt-zeSO7V-4mb6KRNBOVt8hM_aM6A8f1b0hLJyfhtr36NpBjzaMPKs60hFgyC-hEE_KGQwy9quolHFEZL_fu4OGvufzYXjv67HCel70j8&safety_id=AAKH9HGH666PSGDPyU_krf7H
        ts_url = 'https://ali-safety-video.acfun.cn/mediacloud/acfun/acfun_video/' + ts
        # 获取视频的二进制数据
        content = requests.get(url=ts_url, headers=headers).content
        with open('video\\' + f'{title}.mp4', mode='ab') as f:
            f.write(content)
        page += 1
        print(ts_url)
# 源码、解答、教程、安装包等资料加V:xiaoyuanllsll免费领

epilogue

Thank you for reading my article~

I hope this article has been helpful to you and gained some knowledge~

There is no end to learning, and I hope you will become a better version of yourself. (Let's work hard together).
Insert image description here

Guess you like

Origin blog.csdn.net/XM67_/article/details/131861115