Python2 根据知乎小视频的url爬取视频保存到本地

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lcr_happy/article/details/85010337

代码里面注释写的比较清楚:

# -*- coding: utf-8 -*-
#python2

#根据知乎小视频的url爬取视频保存到本地

import requests
#向该地址请求会返回视频的信息,是一个json字符串
lens_url_prefix = "https://lens.zhihu.com/api/videos/"
original_url = 'https://v.vzuu.com/video/1005764681541107712'
def downloadVideo(original_url):
	headers = {
		'User-Agent':'Mozilla/5.0 (Windows NT 10.0; …) Gecko/20100101 Firefox/63.0'
	}
	video_id = original_url.split('/')[-1]
	lens_url = lens_url_prefix + video_id
	#向该地址请求会返回视频的信息,是一个json字符串,ld,sd,hd代表不同的清晰度:普清、标清、高清
	'''
	{
	"playlist": {
		"ld": { 
			"width": 364,
			"format": "mp4",
			"play_url": "https://vdn.vzuu.com/LD/9ce4ca82-9052-11e8-aec1-0242ac112a0d.mp4?auth_key=1541656914-0-0-811f4e000286e9f29ba8c3325ef52506&expiration=1541656914&disable_local_cache=1",
			"duration": 42,
			"size": 2743156,
			"bitrate": 516,
			"height": 640
		},
		"hd": {
			"width": 482,
			"format": "mp4",
			"play_url": "https://vdn.vzuu.com/SD/9ce4ca82-9052-11e8-aec1-0242ac112a0d.mp4?auth_key=1541656914-0-0-9e9f482c769127d24fb303adf8c6ad82&expiration=1541656914&disable_local_cache=1",
			"duration": 42,
			"size": 3768602,
			"bitrate": 709,
			"height": 848
		},
		"sd": {
			"width": 482,
			"format": "mp4",
			"play_url": "https://vdn.vzuu.com/SD/9ce4ca82-9052-11e8-aec1-0242ac112a0d.mp4?auth_key=1541656914-0-0-9e9f482c769127d24fb303adf8c6ad82&expiration=1541656914&disable_local_cache=1",
			"duration": 42,
			"size": 3768602,
			"bitrate": 709,
			"height": 848
		}
	},
	"title": "",
	"duration": 42,
	"cover_info": {
		"width": 482,
		"thumbnail": "https://pic1.zhimg.com/80/v2-ba7669d3646fa73c0c14920c93658994_b.jpg",
		"height": 848
	},
	"type": "video",
	"id": "1005687198120841216",
	"misc_info": {}
	}
	'''
	#我们选择高清的下载地址
	#print lens_url
	response = requests.get(lens_url,headers=headers)
	#print response.text
	json = response.json()
	video_url = json['playlist']['hd']['play_url']
	video_headers={
		'Host':'vdn.vzuu.com',
		'Referer':original_url,
		'User-Agent':'Mozilla/5.0 (Windows NT 10.0; …) Gecko/20100101 Firefox/63.0'
	}
	
	video_response = requests.get(video_url,headers=video_headers)
	with open("test.mp4",'wb') as f:
		f.write(video_response.content)
		f.close()
	print "下载成功"


downloadVideo(original_url)

关键是要多动手实践,找到问题在哪边。祝你好运,有问题欢迎留言,enjoy!


猜你喜欢

转载自blog.csdn.net/lcr_happy/article/details/85010337