如何使用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)
相关推荐
ada7_11 小时前
LeetCode(python)78.子集
开发语言·数据结构·python·算法·leetcode·职场和发展
我送炭你添花11 小时前
Pelco KBD300A 模拟器:06+5.串口实现的逻辑优化、配置管理与协议完善(二次迭代)
python·运维开发
databook11 小时前
前注意加工:让你的图表抓住读者的眼球
python·数据分析·数据可视化
知行学思11 小时前
Python配置管理完全指南:从dotenv到pydantic_settings
数据库·python·fastapi·环境变量·配置管理·pydantic·dotenv
5:0014 小时前
Python进阶语法
开发语言·python
小康小小涵14 小时前
睿抗机器人大赛魔力元宝
python·ubuntu·gitee·github
勇往直前plus15 小时前
Python 类与实例对象的内存存储
java·开发语言·python
禾叙_15 小时前
【canal】canal同步msyql到redis
android·redis·python
先做个垃圾出来………15 小时前
Python位运算及操作
java·前端·python
人工小情绪15 小时前
python报错:AttributeError: module ‘numpy‘ has no attribute ‘object‘.
python·numpy·neo4j