爬ku6网视频

#!/usr/bin/env python
# _*_ coding: utf-8 _*_
# @Time : 2022/9/8 13:08
# @Author : AndyXi
# @Version:V 0.1
# @File : 爬video.py
# @desc :

import requests
import json
from datetime import datetime
from tqdm import tqdm



####通过时间,为下载的文件命名
def get_filename():
return datetime.now().strftime("%Y-%m-%d-%H-%M-%S-%f")


if __name__ == '__main__':

# print(get_filename()
page_size=int(input("请输入你要下载的视频数量: "))
###为了反爬,模拟
header = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36"
}
res_data=requests.get(f"https://www.ku6.com/video/feed?"
f"pageNo=0&pageSize={page_size}&subjectId=76",headers=header)
####如果服务端返回的是字符,则用text
####如果服务端返回的是视频、图片、mp3 等等,这种二进制的用content
res_dict = json.loads(res_data.text)
# print(res_dict["data"])
for item in res_dict["data"]:
# print(item["playUrl"])
video_url=item.get("playUrl","")

#####以下方法局限性能问题,同时没有进度条++++++++++++++++++++++++++++++++++++++
# video_data=requests.get(video_url,headers=header)
#
# with open(f"download_video/{get_filename()}.mp4","wb") as f:
# f.write(video_data.content)
#
#######以下方法加进度条和指定下载流大小++++++++++++++++++++++++++++++++++++++++
video_data=requests.get(video_url,headers=header,stream=True)
if "content-length" in video_data.headers:
###获取了视频的大小,大小为b
content_size=int(video_data.headers["content-length"])
video_name=get_filename() + ".mp4"

with open(f"download_video/{get_filename()}.mp4","wb") as f,tqdm(desc=video_name,total=content_size) as bar:
for chunk in video_data.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
bar.update(len(chunk))
posted @ 2022-09-08 16:53  青空如璃  阅读(36)  评论(0编辑  收藏  举报