From c4ab4dfb71350fcb62fdcad83dab755b17058c7d Mon Sep 17 00:00:00 2001 From: wbt5 Date: Tue, 18 Aug 2020 21:54:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=BF=AB=E6=89=8B=E7=9B=B4?= =?UTF-8?q?=E6=92=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kuaishou.py | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/kuaishou.py b/kuaishou.py index 1372803..19cf8c2 100644 --- a/kuaishou.py +++ b/kuaishou.py @@ -1,25 +1,29 @@ # 获取快手直播的真实流媒体地址,默认输出最高画质 -import requests import json import re - -def get_real_url(rid): - try: - room_url = 'https://m.gifshow.com/fw/live/' + str(rid) - headers = { - 'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1', - 'cookie': 'did=web_'} - response = requests.get(url=room_url, headers=headers).text - m3u8_url = re.findall(r'type="application/x-mpegURL" src="([\s\S]*?)_sd1000(tp)?(/index)?.m3u8', response)[0] - real_url = [m3u8_url[0] + i for i in ['.flv', '.m3u8']] - except: - real_url = '该直播间不存在或未开播' - return real_url +import requests -rid = input('请输入快手直播间ID:\n') -real_url = get_real_url(rid) -print('该直播源地址为:') -print(real_url) +def kuaishou(rid): + headers = { + 'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 ' + '(KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1', + 'cookie': 'did=web_'} + with requests.Session() as s: + res = s.get('https://m.gifshow.com/fw/live/{}'.format(rid), headers=headers) + livestream = re.search(r'liveStream":(.*),"obfuseData', res.text) + if livestream: + livestream = json.loads(livestream.group(1)) + *_, hlsplayurls = livestream['multiResolutionHlsPlayUrls'] + urls, = hlsplayurls['urls'] + url = urls['url'] + return url + else: + raise Exception('直播间不存在或未开播') + + +if __name__ == '__main__': + r = input('输入快手直播房间号:\n') # 例:jjworld126 + print(kuaishou(r))