qq解析代码碎片

本文介绍了一个用于解析腾讯视频VIP内容的Python脚本。通过发送请求获取视频真实URL,并使用BeautifulSoup进行HTML解析。该脚本能从指定的腾讯视频链接中提取视频标题和播放地址。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



def qqVipParse(url):
    # don't use 'lxml', By default, lxml's parser will strip CDATA sections from the tree and
    # replace them by their plain text content., more https://groups.google.com/forum/?fromgroups=#!topic/beautifulsoup/whLj3jMRq7g
    soup = BeautifulSoup(requests.get(url).content, "html.parser")  # requests自动解码有问题,取content交给bs解码
    videoTitle = soup.title.text.split(' -', maxsplit=1)[0]
    # print(repr(videoTitle))
    r = requests.get('http://vip.zhanjh.com/qq.asp', params={'url': url.split('?', maxsplit=1)[0], 'hao': '456jh'}, headers={  # url.split是为了除去参数
        'Referer': 'http://www.yaokanla.com',
        # 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36',
        # 'Host': 'vip.zhanjh.com'
    })
    soup = BeautifulSoup(r.content, "html.parser")
    videoUrl = soup.find('iframe').get('src').split('=', maxsplit=1)[-1]
    if '"\n' == videoUrl:
        raise  # 没有地址就激起错误,以便后面好处理


    # print(videoUrl)
    fileExt = os.path.splitext(urllib.parse.urlparse(videoUrl).path)[-1]
    # print('----baseName------fileExt-------', repr(baseName), repr(fileExt))
    fileName = videoTitle + (fileExt or '.mp4')
    return (fileName, [videoUrl])



def parse(queryDict):
    try:
        resultDict = {'parseResult': qqVipParse(queryDict['sourceUrl'])}
    except Exception as e:
        # logging.error(e)  # 把you-get出错的信息发到log里面去
        resultDict = {'parseResult': None, 'reason': '该页面无法找到视频'}
        raise e
    finally:
        # 解析成功就返回dict,否则 ''
        print('Parsing resultDict------------', resultDict)
        return resultDict




if __name__ == '__main__':
    urls = ['http://v.qq.com/cover/w/wussyc1mi5f6fkx.html?vid=m0014ofzky5',  # 神雕侠侣动画合集版72
            'https://v.qq.com/x/cover/d85gvl439e98ih0.html?ptag=baidu.aladdin.movie.pay',  # VIP 十月的天空
            'http://v.qq.com/error.html',  # 404错误:你访问的页面丢失了
            'http://v.qq.com/detail/w/wussyc1mi5f6fkx.html',  # 专辑页面,也不能解析
            'http://www.iqiyi.com/v_19rr9s226s.html?pltfm=11&pos=title&flashvars=videoIsFromQidan%3Ditemviewclk_a#vfrm=5-6-0-1',
            ]
    for url in urls:
        parse({'sourceUrl': url})

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值