2015-11-24 18:57:22
2015年12月更新:兰大直播源全新改版,采用HLS协议传送,原VLC全面弃用,该脚本作废
import httplib
import os
conn = httplib.HTTPConnection('hdtv.lzu6.edu.cn')
conn.request('GET','/?post=1','',{'User-Agent':'Chrome/46.0.2490.80'})
response = conn.getresponse()
conn.close()
res = response.read().split('\n')
t = open('tmp','w+')
for line in res:
if 'cn:8' in line:
s0 = line.replace('<span style="font-family:SimSun;font-size:16px;">','#EXTINF:0,')
s1 = s0.replace(' ','')
s2 = s1.replace('</span>','')
s3 = s2.replace('<br />','')
s4 = s3.replace('<span style="font-family:SimSun;font-size:16px;line-height:1.5;">','')
s5 = s4.replace('<span style="font-size:16px;">','')
s6 = s5.replace('<span style="font-family:SimSun;font-size:16px;line-height:24px;white-space:normal;">','')
s7 = s6.strip()
t.write(s7 + '\n')
t.seek(0)
f = open('TVlzu.m3u','w+')
s = t.readline()
#print r
while len(s):
sa = s[:-30].strip()
sb = s[-30:].strip()
f.write(sa + '\n')
f.write(sb + '\n')
s = t.readline()
t.close()
f.close()
os.remove('tmp')
主要原理:
Get到节目列表的网页源代码,截取节目列表所在的行并格式化成vlc识别的列表文件
介绍函数:
httplib 用于建立http,https等协议连接,获取response
下属有HTTPConnection,下属函数request(),getresponse()
request(Method,url,body,header)
open(filename,arg) arg:[r,r+,w,w+,a,a+,b]
文件不存在即创建
字符串逐行处理:
字符串写在文件中才有“逐行”的概念。而存在内存中的为“序列”
所以字符串对象没有readline()函数,可以后接split(’\n’)代替;
s.strip(rm) 删除字符串s首尾的rm序列。类似有lstrip,rstrip函数;
字符串也是一种“序列”,可以进行截取。如本例中的s[ : -30](s[0:-30])表示截取从字符串首到倒数第三十个字符处。
注意:如截取到串尾 冒号后留空即可s[5:],s[-0]和s[0]是同位(0=-0)