学习python 抓取知乎指定回答下视频的方法

发布时间:2024-06-06 点击:92
前言
现在知乎允许上传视频,奈何不能下载视频,好气哦,无奈之下研究一下了,然后撸了代码,方便下载视频保存。
接下来以 猫为什么一点也不怕蛇? 回答为例,分享一下整个下载过程。
相关学习推荐:python视频教程
调试一下
打开 f12, 找到光标,如下图:
{ "playlist": { "ld": { "width": 360, "format": "mp4", "play_url": "https://vdn.vzuu.com/ld/05fc411e-d8e0-11e8-bb8b-0242ac112a0b.mp4?auth_key=1541477643-0-0-987c2c504d14ab1165ce2ed47759d927&expiration=1541477643&disable_local_cache=1", "duration": 17, "size": 1123111, "bitrate": 509, "height": 640 }, "hd": { "width": 720, "format": "mp4", "play_url": "https://vdn.vzuu.com/hd/05fc411e-d8e0-11e8-bb8b-0242ac112a0b.mp4?auth_key=1541477643-0-0-8b8024a22a62f097ca31b8b06b7233a1&expiration=1541477643&disable_local_cache=1", "duration": 17, "size": 4354364, "bitrate": 1974, "height": 1280 }, "sd": { "width": 480, "format": "mp4", "play_url": "https://vdn.vzuu.com/sd/05fc411e-d8e0-11e8-bb8b-0242ac112a0b.mp4?auth_key=1541477643-0-0-5948c2562d817218c9a9fc41abad1df8&expiration=1541477643&disable_local_cache=1", "duration": 17, "size": 1920976, "bitrate": 871, "height": 848 } }, "title": "", "duration": 17, "cover_info": { "width": 720, "thumbnail": "https://pic2.zhimg.com/80/v2-97b9435a0c32d01c7c931bd00120327d_b.jpg", "height": 1280 }, "type": "video", "id": "1039146361396174848", "misc_info": {}}没错了,我们要下载的视频就在这里面,其中 ld 代表普清,sd 代表标清, hd 代表高清,把相应链接再次在浏览器打开,然后右键保存就可以下载视频了。
代码
知道整个流程是什么样子,接下来撸代码的过程就简单了,这里就不过再做过多解释了,直接上代码:
# -*- encoding: utf-8 -*-import reimport requestsimport uuidimport datetimeclass downloadvideo: __slots__ = [ 'url', 'video_name', 'url_format', 'download_url', 'video_number', 'video_api', 'clarity_list', 'clarity' ] def __init__(self, url, clarity='ld', video_name=none): self.url = url self.video_name = video_name self.url_format = "https://www.zhihu.com/question/\\\\d /answer/\\\\d " self.clarity = clarity self.clarity_list = ['ld', 'sd', 'hd'] self.video_api = 'https://lens.zhihu.com/api/videos' def check_url_format(self): pattern = re.compile(self.url_format) matches = re.match(pattern, self.url) if matches is none: raise valueerror( "链接格式应符合:https://www.zhihu.com/question/{number}/answer/{number}" ) return true def get_video_number(self): try: headers = { 'user-agent': 'mozilla/5.0 (macintosh; intel mac os x 10_12_6) applewebkit/537.36 (khtml, like gecko) chrome/66.0.3359.181 safari/537.36' } response = requests.get(self.url, headers=headers) response.encoding = 'utf-8' html = response.text video_ids = re.findall(r'data-lens-id="(\\\\d )"', html) if video_ids: video_id_list = list(set([video_id for video_id in video_ids])) self.video_number = video_id_list[0] return self raise valueerror("获取视频编号异常:{}".format(self.url)) except exception as e: raise exception(e) def get_video_url_by_number(self): url = "{}/{}".format(self.video_api, self.video_number) headers = {} headers['referer'] = 'https://v.vzuu.com/video/{}'.format( self.video_number) headers['origin'] = 'https://v.vzuu.com' headers[ 'user-agent'] = 'mozilla/5.0 (windows nt 10.0; wow64) applewebkit/537.36 (khtml, like gecko) chrome/70.0.3538.67 safari/537.36' hea

国内镜像服务器相关云计算内容
docker镜像到底是什么
怎么设计才能让人对网站产生信赖
堡垒机供应商
如何购买弹性云服务器ecs
企业如何建网站才能在宣传方面一劳永逸
沈阳云服务器租赁
域名.cn和.com的区别体现在哪里?