Python

Python处理腾讯视频

2019-03-04  本文已影响0人  Donald_32e5

背景

分析网站(业务没用上)

通过分析网站发现,视频都是一段段的请求的,然后组合起来,展示视频

-5、贴上源码,以备不测

from urllib import request
import json
import QiniuClient


def down(download_url, video_name):
    request.urlretrieve(download_url, filename=video_name, reporthook=report, data=None)


def report(a, b, c):
    """
    a:已经下载的数据块
    b:数据块的大小
    c:远程文件的大小
    """
    per = 100.0 * a * b / c
    if per > 100:
        per = 100
    print('%.2f%%' % per)



template_url = 'http://vv.video.qq.com/getinfo?vids=%s&platform=101001&charge=0&otype=json&defn=shd'


def tecent_parse(target_url):
    vid = target_url[len('https://v.qq.com/x/page/'):-5]
    response = request.urlopen(request.Request(template_url % vid)).read().decode('utf-8')
    json_data = response[len('QZOutputJson='):-1]
    json_obj = json.loads(json_data)
    download_url = json_obj['vl']['vi'][0]['ul']['ui'][0]['url'] + json_obj['vl']['vi'][0]['fn'] + '?vkey=' + \
                   json_obj['vl']['vi'][0]['fvkey']
    video_name = json_obj['vl']['vi'][0]['ti'] + "." + json_obj['fl']['fi'][1]['name']
    return download_url, video_name


if __name__ == '__main__':
    url = 'https://v.qq.com/x/page/k0815w0wxm9.html'
    if 'https://v.qq.com/x/page/' in url:
        down_url, vid_name = tecent_parse(url)
    else:
        print("it's not supported")
    down(down_url, vid_name)
    file_key = QiniuClient.upload_local_file(QiniuClient.QiniuClient.UPLOAD_VIDEO, vid_name)
    cover_link = QiniuClient.get_full_path(file_key)
    print(cover_link)

再记录下使用iframe的坑

# 这个链接拼接上vid之后,会自适应iframe的宽高
https://v.qq.com/iframe/preview.html?auto=0&vid={}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body style="text-align: center">
<br>
<p>{}</p>
<br>
<br>
<div>
    <iframe style="width: 100%; height: 550px" class="div" frameborder="1" src={} allowFullScreen="true"></iframe>
</div>
</body>
</html>
上一篇 下一篇

猜你喜欢

热点阅读