兰州大学hdtv列表文件 python 自动生成脚本

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('&nbsp;','')
		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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值