最近发现ks的直播间Wss一直在变化,废话不多说直接上代码,希望能够帮助到小白们
import lxml.etree, json
import requests
url_api = 'https://live.ks(记得修改).com/live_graphql'
url_api_search = 'https://www.(你懂的).com/graphql'
url = input("输入你要查询的网址:")
headers = {
'Referer': url,
'content-type': 'application/json',
'Origin': 'https://www.xxxxx(记得修改).com',
'Cookie': '填上自己的cookie',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
}
resp = requests.get(url=url, headers=headers).text
js = lxml.etree.HTML(resp).find('.//body/script').text
jsonCode = js.partition('liveStreamId":"')[2].strip()
fin_data = jsonCode.split('","caption"')[0] #streamid
data = {
'operationName': "WebSocketInfoQuery",
'query': "query WebSocketInfoQuery($liveStreamId: String) {\n webSocketInfo(liveStreamId: $liveStreamId) {\n token\n webSocketUrls\n __typename\n }\n}\n",
'variables': {'liveStreamId': fin_data} # livestreamid
}
resp = requests.post(url=url_api, headers=headers, json=data)
res_text = resp.json()
res_list = res_text['data']['webSocketInfo']['token'] # token
res_url = res_text['data']['webSocketInfo']['webSocketUrls'][0] # wss链接
print("streamid是:" + fin_data)
print("toke是:" + res_list)
print("wss链接:" + res_url)
# 引入token,livestreamid,wss结束