当前位置: 首页 > 工具软件 > bilibili-user > 使用案例 >

爬虫之-bilibili视频下载-下载链接获取

方和顺
2023-12-01
  • 获取系列视频信息:(名称+请求地址)
urls = []
names = []
bv_num = "这里是需要下载视频的BV值"
def get_tilt():
	http = "https://www.bilibili.com/video/%s?p=%s"
	try:
		url = "https://www.bilibili.com/video/%s" % bv_num
		response = requests.get(url=url, headers=getHtmlHeaders)
		if response.status_code == 200:
			pattern2 = r'\<script\>window\.__INITIAL_STATE__=(.*?);'
			result2 = re.findall(pattern2, response.text)[0]
			title = json.loads(result2)
			for a in title["videoData"]["pages"]:
				p = a["page"]
				part = a["part"]
				urls.append(http % (bv_num, p))
				names.append(part)
	except RequestException:
		print('请求Html错误')
  • 获取视频下载地址:(用于分片下载)
url = "前面获取到各‘集’视频地址"
getHtmlHeaders = {
			'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
						  'Chrome/86.0.4240.198 Safari/537.36'
		}
def parseHtml():
	try:
		response = requests.get(url=url, headers=getHtmlHeaders)
		if response.status_code == 200:
			html = response.text
			try:
				pattern1 = r'\<script\>window\.__playinfo__=(.*?)\</script\>'
				result1 = re.findall(pattern1, html)[0]
				temp = json.loads(result1)
				try:
					video_url = temp["data"]["dash"]["video"][0]["baseUrl"]
					audio_url = temp['data']['dash']['audio'][0]['baseUrl']
					# video_url 、audio_url 后面用于下载
				except:
					vedio_url = temp['data']['durl'][0]['url']
					# video_url 后面用于下载
			except:
				print("%s【%s】下载失败!********" % (title, url))
	except RequestException:
		print('请求Html错误:')
  • 至上我们已经将该系列视频名称及视频、音频下载地址全部获取到了。后面就可以进行分片下载啦!
 类似资料: