Python 执行 MP4视频下载

发布时间 2023-07-16 18:49:10作者: sszqxt
import requests


def extract_video_links(url):
    """ 提取视频链接 """
    response = requests.get(url)
    html = response.text  # 在HTML中查找视频链接
    pattern = r'src="([^"]+\.mp4)"'
    matches = re.findall(pattern, html)
    return matches


def download_video(url, filename):
    """ 下载视频 """
    response = requests.get(url, stream=True)
    with open(filename, 'wb') as file:
        for chunk in response.iter_content(chunk_size=1024):
            if chunk: file.write(chunk)  # 提取视频链接
            url = "https://example.com/video.html"
            links = extract_video_links(url)  # 下载视频
            for link in links:
                filename = link.split("/")[-1]
                download_video(link, filename)


# 在上面的示例代码中,`extract_video_links()`函数使用`requests`库发送HTTP请求,并从HTML中查找视频链接。
# `download_video()`函数使用`requests`库下载视频,并将其保存到本地文件。
# 最后,使用循环遍历所有链接,并调用`download_video()`函数下载每个视频。请注意,示例代码仅作为参考,实际应用中需要根据具体情况进行修改。