如何使用Python进行下载对应的视频地址

如何使用Python进行下载对应的视频地址

下载 视频地址

python 复制代码
import requests


def download_video(video_url, save_path):
    try:
        response = requests.get(video_url)
        if response.status_code == 200:
            with open(save_path, 'wb') as f:
                f.write(response.content)
            print(f"视频下载成功,保存至 {save_path}")
        else:
            print(f"下载失败,状态码: {response.status_code}")
    except requests.exceptions.RequestException as e:
        print(f"下载过程出现错误: {e}")


if __name__ == "__main__":
    video_url = "这里替换为你实际获取到的视频地址"
    save_path = "video.mp4"  # 可以根据需要修改保存的文件名和路径
    download_video(video_url, save_path)

批量处理

python 复制代码
import certifi
import requests
import csv


def read_csv(csvfile):
    urls=[]
    # 打开csv文件
    with open(csvfile, mode='r', newline='') as file:
        # 创建csv读取器
        csvR = csv.reader(file)
        # 遍历每一行,跳过第一行
        for row in csvR:
            url = row[0]
            urls.append(url)
    # urls = list(set(urls))
    unique_list = []
    for i in urls:
        if i not in unique_list:
            unique_list.append(i)
    print(unique_list)
    return unique_list

def write_csv(csvfileW,dataW):
    # 打开csv文件以写入模式
    with open(csvfileW, mode='w', newline='') as fileW:
        # 创建csv写入器
        csvW = csv.writer(fileW)
        # 写入数据
        for row in dataW:
            csvW.writerow(row)

def download_video(video_url, save_path):
    try:
        # response = requests.get(video_url,verify=certifi.where())
        response = requests.get(video_url,verify=False)
        if response.status_code == 200:
            with open(save_path, 'wb') as f:
                f.write(response.content)
            print(f"视频下载成功,保存至 {save_path}")
        else:
            print(f"下载失败,状态码: {response.status_code}")
    except requests.exceptions.RequestException as e:
        print(f"下载过程出现错误: {e}")




if __name__ == "__main__":
    urls = read_csv(r'fiddler.csv')

    n = 0
    for video_url in urls:
        n+=1
        save_path = r"{}.mp4".format(n)
        download_video(video_url, save_path)
相关推荐
楚疏笃35 分钟前
纯Python 实现 Word 文档转换 Markdown
python·word
谅望者44 分钟前
数据分析笔记08:Python编程基础-数据类型与变量
数据库·笔记·python·数据分析·概率论
mortimer1 小时前
【实战复盘】 PySide6 + PyTorch 偶发性“假死”?由多线程转多进程
pytorch·python·pyqt
清静诗意1 小时前
Django REST Framework(DRF)RESTful 最完整版实战教程
python·django·restful·drf
studytosky1 小时前
深度学习理论与实战:Pytorch基础入门
人工智能·pytorch·python·深度学习·机器学习
长不大的蜡笔小新2 小时前
手写数字识别:从零搭建神经网络
人工智能·python·tensorflow
前进的李工2 小时前
LeetCode hot100:094 二叉树的中序遍历:从递归到迭代的完整指南
python·算法·leetcode·链表·二叉树
ins_lizhiming3 小时前
在华为910B GPU服务器上运行DeepSeek-R1-0528模型
人工智能·pytorch·python·华为
bwz999@88.com3 小时前
win10安装miniforge+mamba替代miniconda
python