腾讯视频网页解析python版

September 29, 2018 · 分享 · python · 145次阅读

# coding: utf-8

# In[1]:


import requests
import json
import time
import hashlib
import re

# In[2]:


def getckey(vid, platform):
    tm = str(int(time.time()))
    string = "4244ce1b{}{}*#06#10201".format(vid, tm)#encryptVer=7.2
    ckey = hashlib.md5(string.encode('utf-8')).hexdigest()
    flowid = "{}_{}".format(hashlib.md5(tm.encode('utf-8')).hexdigest(), platform)
    return tm, ckey, flowid


# In[3]:


def proxyhttp(url, data):
    s = requests.Session()
    s.headers.update({"user-agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"})
    s.headers.update({"referer":url})

    cookie = "your cookie"
    c = {}
    for _ in cookie.split(';'):
        key,value = _.strip().split('=',1)
        c.update({key:value})
    requests.utils.add_dict_to_cookiejar(s.cookies,c)
    r = s.post("https://vd.l.qq.com/proxyhttp", data=data)
    return (r)


# In[4]:


def getdata(url,vid, coverid, guid, unid, sdtfrom):
    platform = ["10201"]
    tm, ckey, flowid = getckey(vid, platform[0])
    defn = ["fhd","shd","hd","sd"]
    data = {
        "buid": "onlyvinfo",
        "vinfoparam": "charge=0&defaultfmt=auto&otype=ojson&guid={}&flowid={}"
        "&platform=10201&sdtfrom=v1010&defnpayver=1&appVer=3.5.56&host=v.qq.com"
        "&ehost={}&refer=v.qq.com&sphttps=1&tm={}&spwm=4&unid={}&vid={}&defn={}"
        "&fhdswitch=1&show1080p=1&isHLS=1&onlyGetinfo=true&dtype=3&sphls=2&spgzip=1"
        "&dlver=2&defsrc=2&encryptVer=7.2&cKey={}&fp2p=1".format(guid,flowid,url,tm,unid,vid,'fhd',ckey)
    }
    return json.dumps(data)


# In[5]:


def getid(url):
    mode = [
        '.*v\.qq\.com/x/cover/(.*)\.html\?vid=(.*)',
        '.*v\.qq\.com/x/cover/(.*)/(.*)\.html',
        '.*v\.qq\.com/x/page/(.*)\.html',
    ]
    for _ in mode:
        matchres = re.match(_, url)
        if matchres == None:
            pass
        else:
            matchres = matchres.groups()
            vid = matchres[-1]
            if len(matchres) == 2:
                coverid = matchres[0]
            else:
                coverid = ""
    return vid, coverid


# In[6]:


def dl_qq(url):
    guid = "5ab01a7ab3b68b62787b5d1df507ba5c"#固定值
    unid = "1cbe987dbf1111e89d19a0424b63310a"#也是一个固定值
    sdtfrom = "v1010"
    #url = "https://v.qq.com/x/cover/y4g7p8eka7bshz3/i0544naygqq.html"
    vid, coverid = getid(url)
    # vid = "w00273lvxjp"
    # coverid = "0913mhn6zhnk66b"
    data = getdata(url, vid, coverid, guid, unid, sdtfrom)
    res = proxyhttp(url, data)
    resdict = json.loads(res.content.decode('utf-8'))
    resdict.get("errCode")
    if resdict.get("errCode") == 0:
        pass
    else:
        print(resdict.get("errCode"))
    resdict = json.loads(resdict["vinfo"])['vl']['vi'][0]
    title = re.sub('[\/:*?"<>|]','',resdict.get("ti"))
    urllist = resdict.get("ul").get("ui")
    m3u8_url = urllist[-1].get("url")
    type = "m3u8"
    if ".m3u8" in m3u8_url:
        pass
    else:
        m3u8_url = m3u8_url + urllist[-1].get("hls").get("pt")
    screensize = "{}x{}".format(resdict.get("vw"),resdict.get("vh"))
    try:
        vkey = resdict["fvkey"]
    except KeyError:
        urllist = [m3u8_url]
        print(title, screensize)
        print(m3u8_url)
    else:
        type = "list"
        urllist = []
        if len(resdict["cl"]["ci"]) == 1:
            keyid = resdict["cl"]["ci"][0]["keyid"]
            keyid = keyid.replace(".10",".p")
            directlink = "{}{}.mp4?sdtfrom={}&guid={}&vkey={}".format(m3u8_url, keyid, sdtfrom, guid, vkey)
            print(directlink)
            urllist.append(directlink)
        else:
            pass
    dlinfo = {"title":title,"url":urllist,"type":type,"referer":"https://v.qq.com"}
    print(dlinfo)
    return dlinfo
    
def main():
    url = input("请输入链接:\n")
    dl_qq(url)
        
if __name__ == "__main__":
    main()

标签:none

最后编辑于:2018/09/29 07:43

添加新评论