headers = { 'referer': referer_url }
在视频播放网页打开播放的请求发现请求头中有referer
防盗链
如果我们直接发起请求会返回以下错误结果:
所以需要在headers
中设置referer
import random import time import requests from fake_useragent import UserAgent def get_rel_link(cont_id: str): url = 'https://www.pearvideo.com/videoStatus.jsp' param = { 'contId': cont_id, 'mrd': round(random.random(), 16) } headers = { 'user-agent': UserAgent().random, 'referer': 'https://www.pearvideo.com/video_' + cont_id } res = requests.get(url=url, headers=headers, params=param) vedio_info = res.json().get('videoInfo') # 真视频地址 'https://video.pearvideo.com/mp4/adshort/20220111/cont-1749725-15815507_adpkg-ad_hd.mp4' # 伪视频地址 'https://video.pearvideo.com/mp4/adshort/20220111/1645089594693-15815507_adpkg-ad_hd.mp4' faka_vedio_url = vedio_info.get('videos').get('srcUrl') # 分割 temp1 = faka_vedio_url.split('-', 1) # 前部分翻转 temp2 = ''.join(reversed(temp1[0])) # 翻转后以'/'切割再翻转回来找到假id fake_cont_id = ''.join(reversed(temp2.split('/', 1)[0])) rel_vedio_url = faka_vedio_url.replace(fake_cont_id, 'cont-'+cont_id) print('视频地址:', rel_vedio_url) return rel_vedio_url def download_vedio(cont_id: str, rel_vedio_url: str): page_content = requests.get(rel_vedio_url).content with open('视频{}.mp4'.format(cont_id), 'wb') as fp: fp.write(page_content) if __name__ == '__main__': cont_id = input('输入视频网址链接:\n').split('_')[1] start_time = time.time() rel_vedio_url = get_rel_link(cont_id=cont_id) download_vedio(cont_id, rel_vedio_url) end_time = time.time() print("视频下载完成,总耗时:{} s".format(end_time-start_time))
版权属于:瞌学家 所有,转载请注明出处
本文链接:https://songonline.top/archives/146/
友情提示: 如果文章部分链接出现404,请留言或者联系博主修复。