如何使用Python进行下载对应的视频地址
下载 视频地址
import requestsdef 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)
批量处理
import certifi
import requests
import csvdef 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_listdef 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 = 0for video_url in urls:n+=1save_path = r"{}.mp4".format(n)download_video(video_url, save_path)