如何用python下载小型动漫网站视频(适用于新手,附源码)

调出开发者模式及其使用方法

右键点击检查

F12

Ctrl+Shift+i

再选择Network下的XHR或者Media

开发者工具详解看此处:https://zhuanlan.zhihu.com/p/231865779#:~:text=%E5%9C%A8%E5%85%83%E7%B4%A0%E7%9A%84%E5%8F%B3%E9%94%AE%E8%8F%9C%E5%8D%95%E4%B8%AD%E9%80%89%E6%8B%A9%E6%96%AD%E7%82%B9%E9%80%89%E9%A1%B9%EF%BC%88Break%20o

 网站视频的格式

有m3u8格式的,有m4s类型的,还有一些是能够直接使用开发者工具找到原视频地址的。此处详细介绍用python实现m3u8格式的批量获取并合并成一个MP4格式。

双击图中的m3u8格式进行下载

对下载的m3u8文件用记事本打开,如下图所示:

可以观察到每一个ts格式都是在进行加一,待会批量下载将会用到。 

具体操作方法看此链接:https://astr.ac.cn/note/1441/

对小型动漫网站的批量下载与合成MP4格式

直接上代码进行说明:

import os
import requests
from concurrent.futures import ThreadPoolExecutor
from os.path import expanduser
from pyquery import PyQuery as pq

base_url = "https://vip.ffzy-play3.com/20230412/10868_2dee1b0a/2000k"
request_url = r'https://www.yhdmz.org/vp/23165-1-0.html'
html = requests.get(request_url).text
title = pq(html)('head > title').text()
start_index = 0
end_index = 1227
desktop_path = os.path.join(expanduser("~"), "Desktop")
path = fr"{desktop_path}\\video"
output_filename = fr"{desktop_path}\\{title}.mp4"

if not os.path.exists(path):
    os.makedirs(path)
def download_file(index):
    url = f"{base_url}/hls/849b6d7a1cc{index:06d}.ts"
    response = requests.get(url, timeout=30)
    if response.status_code == 200:
        with open(f"{path}\\{index}.ts", "wb", buffering=8192) as f:
            f.write(response.content)
        print(f"Downloaded {url}")
    else:
        print(f"Failed to download {url}\n")
def merge_files(output_filename, start_index, end_index):
    with open(output_filename, "ab") as output_file:
        for index in range(start_index, end_index + 1):
            input_filename = f"{path}\\{index}.ts"
            with open(input_filename, "rb") as input_file:
                output_file.write(input_file.read())
def remove_files(path):
    for index in range(start_index, end_index + 1):
        input_filename = f"{path}\\{index}.ts"
        os.remove(input_filename)
    os.removedirs(path)

with ThreadPoolExecutor(max_workers=120) as executor:
    executor.map(download_file, range(start_index, end_index + 1))
for index in range(start_index, end_index + 1):
    if not os.path.exists(fr"{path}\\{index}.ts"):
        url = f"{base_url}/hls/849b6d7a1cc{index:06d}.ts"
        response = requests.get(url, timeout=20)
        if response.status_code == 200:
            with open(fr"{path}\\{index}.ts", 'wb') as f:
                f.write(response.content)

merge_files(output_filename, start_index, end_index)
remove_files(path)
print(f"All files merged into {output_filename}")

图中的requests请求库需要自己在cmd中输入pip install requests。

图中的pyquery解析库需要自己在cmd中输入pip install pyquery

再对图中用红圈标记出来的,任意单击一个并进行复制,结果如下图所示

 将红圈中hls以前的设为base_url。

start_index为起始ts文件(000000.ts),将其赋值为0。

end_index为最终ts文件(001227.ts),将其赋值为1227。

if not os.path.exists(r'C:\Users\zombieman\Desktop\video'):
    os.makedirs(r'C:\Users\zombieman\Desktop\video')

此段代码为批量下载ts文件夹的路径,我将其文件夹放置在桌面上。

output_filename = f"C:\\Users\\zombieman\\Desktop\\merged_file.mp4"

此段代码为合成ts文件后的mp4文件地址,我也将其放置在桌面上。

出现此结果表示下载完成。桌面上将会出现一个文件和文件夹。

 如有错误或者改进之处,欢迎各位指正

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值