如何使用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)
相关推荐
允许部分打工人先富起来1 小时前
在node项目中执行python脚本
前端·python·node.js
IVEN_1 小时前
Python OpenCV: RGB三色识别的最佳工程实践
python·opencv
haosend2 小时前
AI时代,传统网络运维人员的转型指南
python·数据网络·网络自动化
曲幽2 小时前
不止于JWT:用FastAPI的Depends实现细粒度权限控制
python·fastapi·web·jwt·rbac·permission·depends·abac
IVEN_20 小时前
只会Python皮毛?深入理解这几点,轻松进阶全栈开发
python·全栈
Ray Liang21 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
AI攻城狮21 小时前
如何给 AI Agent 做"断舍离":OpenClaw Session 自动清理实践
python
千寻girling21 小时前
一份不可多得的 《 Python 》语言教程
人工智能·后端·python
AI攻城狮1 天前
用 Playwright 实现博客一键发布到稀土掘金
python·自动化运维