# coding=utf-8 import requests import json import re import os import urlparse import random vid = re.search(r'/(d+).html','http://www.mgtv.com/v/10/167938/c/2942052.html').group(1) r = requests.get('http://v.api.mgtv.com/player/video?retry=1&video_id=%s' % vid) data = json.loads(r.content) streams = data['data']['stream'] stream_domains = data['data']['stream_domain'] for stream in streams: print stream['name'] stream_domain = random.choice(stream_domains) url = urlparse.urljoin(stream_domain, stream['url']) r = requests.get(url) data = json.loads(r.content) m3u8_url = data['info'] parse_result = urlparse.urlparse(m3u8_url) r = requests.get(m3u8_url) segments = re.findall(r'^[^#][^ ][^ ]*', r.content, re.MULTILINE) for s in segments: print '%s://%s%s/%s' % (parse_result.scheme, parse_result.netloc, os.path.dirname(parse_result.path), s)
转载:http://www.jianshu.com/p/c5d6012ec8e5