[Python] 纯文本查看 复制代码import requests
import time
import hashlib
import re
import json
headers = {
'User-Agent': 'Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1'
}
# 测试地址:
# cctv_url = 'https://v.cctv.com/2020/05/31/VIDELxZ6fRrVKzIh0cHiR8v3200531.shtml'
cctv_url = input('请输入视频地址:')
rs = requests.get(cctv_url, headers)
rs.encoding = 'utf-8'
html_data = rs.text
#
title = re.search('
(.*?)', html_data).group(1)print('主题:%s' % title)
# 解决平台特殊符号问题
new_title = title.replace('/', ' ').replace(':', ':').replace('*', ' ') \
.replace('?', '?').replace('', ' ').replace('|', ' ').replace('\\', ' ')
#
rec = re.compile('var guid = "(.*?)";')
content = re.search(rec, html_data)
# 求出vc
guid = content.group(1)
vdn_tsp = str(int(time.time()))
vdn_vn = "2049"
vdn_vc = ""
staticCheck = "47899B86370B879139C08EA3B5E88267"
vdn_uid = ""
#
new_str = (vdn_tsp + vdn_vn + staticCheck + vdn_uid)
vc = hashlib.md5(new_str.encode(encoding='UTF-8')).hexdigest().upper()
# 接口
url_data = f'pid={guid}&tai=ipad&from=html5&tsp={vdn_tsp}&vn=2049&vc={vc}&uid=&wlan='
url = 'https://vdn.apps.cntv.cn/api/getIpadVideoInfo.do?%s' % url_data
res = requests.get(url=url, headers=headers)
print(res.status_code)
data = res.text
# 处理请求
start = data.find("'{")
end = data.find(";")
j_data = json.loads(data[start + 1:end - 1])
#
video = j_data['video']
# 保存到文件
with open('%s.txt' % new_title, 'w', encoding='utf-8') as ff:
# m3u8地址
ff.write('m3u8地址:\n')
ff.write(j_data['hls_url'] + '\n\n')
ff.write('MP4地址:\n')
for value in video:
vv = video[value]
if type(vv) == list:
for u in vv:
video_url = u['url']
print(video_url)
ff.write(video_url + '\n')