下载梨视频

import time
import requests


# 返回视频的真实下载地址
def get_real_video_url(video_id):
    # 视频地址解析接口
    url = 'https://www.pearvideo.com/videoStatus.jsp?contId={}'.format(video_id)
    headers = {
        'Referer': 'https://www.pearvideo.com/video_{}'.format(video_id),  # 防盗链,溯源这次请求的上一次请求的url
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36'
    }
    response = requests.get(url, headers=headers)
    system_time = response.json()['systemTime']
    # 假链接 https://video.pearvideo.com/mp4/adshort/20211111/1637987795364-15793700_adpkg-ad_hd.mp4
    fake_src_url = response.json()['videoInfo']['videos']['srcUrl']
    # 真链接 https://video.pearvideo.com/mp4/adshort/20211111/cont-1746412-15793700_adpkg-ad_hd.mp4
    real_src_url = fake_src_url.replace(system_time, 'cont-{}'.format(video_id))
    return real_src_url


def download_video(url):
    video_id = url.split('-')[1]
    # file_name = str('.\\Video\\') + str(video_id) + '.mp4'
    file_name = str(video_id) + '.mp4'
    try:
        with open(file_name, mode='wb') as f:
            f.write(requests.get(url).content)
        print(video_id, 'success')
    except:
        print(video_id, 'error')


if __name__ == '__main__':
    # url = input('输入视频的url:')
    start = time.time()
    url = 'https://www.pearvideo.com/video_1746036'
    v_id = url.split('_')[1]
    real_url = get_real_video_url(v_id)
    download_video(real_url)
    end = time.time()
    print('用时{:.2f}秒'.format(end - start))
posted @ 2021-12-04 11:38  xuecl  阅读(64)  评论(0编辑  收藏  举报