如何使用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)
相关推荐
站大爷IP7 分钟前
Python构建MCP服务器:从工具封装到AI集成的全流程实践
python
深盾安全2 小时前
Python 装饰器详解
python
前端小趴菜052 小时前
python - 数据类型转换
python
跟橙姐学代码3 小时前
学Python必须迈过的一道坎:类和对象到底是什么鬼?
前端·python
卡洛斯(编程版3 小时前
(1) 哈希表全思路-20天刷完Leetcode Hot 100计划
python·算法·leetcode
FreakStudio3 小时前
一文速通 Python 并行计算:教程总结
python·pycharm·嵌入式·面向对象·并行计算
群联云防护小杜3 小时前
从一次 DDoS 的“死亡回放”看现代攻击链的进化
开发语言·python·linq
Ice__Cai4 小时前
Flask 入门详解:从零开始构建 Web 应用
后端·python·flask·数据类型
霸敛4 小时前
好家园房产中介网后台管理完整(python+flask+mysql)
开发语言·python·flask
HenryLin4 小时前
SHAP值的核心概念
python