import json import re import requests from bs4 import BeautifulSoup def getUrl(playurl): # 请求头 headers = { 'X-Requested-With': 'XMLHttpRequest', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) ' 'Chrome/56.0.2924.87 Safari/537.36' } # headers['referer']='http://www.miguvideo.com/wap/resource/pc/detail/miguplay.jsp?cid=650644142' baseurl = 'www.miguvideo.com/wap/resource/pc/detail/miguplay.jsp' try: if re.match("^(http|https):\/\/www\.miguvideo\.com\/wap\/resource\/pc\/detail\/miguplay\.jsp\?cid=\d+$",playurl): req = requests.get(playurl, headers=headers) sessionID = 0 playId = 0 # 提取接口参数 cid = re.findall('\d+', playurl)[0] # 获取sessionID result={'msg':'网络异常'} if req.status_code == 200: document = BeautifulSoup(req.text, 'lxml') print('sessionID=%s' % sessionID) sessionID = document.select('#sessionID')[0].get('value') print('sessionID=%s' % sessionID) # 获取playId if req.status_code == 200: req = requests.get('http://www.miguvideo.com/wap/resource/pc/data/miguData.jsp?cid=%s' % cid, headers=headers) miguData = json.loads(req.text) print('playId=%s' % playId) playId = miguData[0]['playId'] print('playId=%s' % playId) # 使用播放地址接口获取视频信息 req = requests.get( 'http://www.miguvideo.com/playurl/v1/play/playurlh5?contId=%s&rateType=1,2,3&clientId=%s' % ( playId, sessionID)) if req.status_code == 200: videoInfo = json.loads(req.text) # 解析出json结构视频信息,获取视频真实地址 result = {'name': miguData[0]['Detail'],'video':[]}; print("视频信息=%s" % miguData[0]['Detail']) if videoInfo['code'] == '200' and 'body' in videoInfo and 'urlInfos' in videoInfo['body']: for info in videoInfo['body']['urlInfos']: result['video'].append({'rateDesc':info['rateDesc'],'url':info['url'],'mediaSize':info['mediaSize']}) print('清晰度=%s' % info['rateDesc']) print('真实地址=%s' % info['url']) print('视频大小=%s字节' % info['mediaSize']) else: result = {'msg': '不是合法播放地址'} except BaseException as e: result={'msg':'程序异常'} return result