免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 9278 | 回复: 19
打印 上一主题 下一主题

中央电视台节目单 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-08-09 20:54 |只看该作者 |倒序浏览
本帖最后由 two_cu 于 2010-10-07 19:27 编辑

  1. #! /usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import urllib,re,unicodedata,string,sys
  4. from time import strftime,localtime
  5. channel={"1":"CCTV1","2":"CCTV2","3":"CCTV3","4":"CCTV4",
  6.         "5":"CCTV5","6":"CCTV6","7":"CCTV7","8":"CCTV8",
  7.         "9":"CCTV9","10":"CCTV10","11":"CCTV11","12":"CCTV12",
  8.         "13":"cctv13","Asia":"cctvAsia","Europe":"cctvEurope","Americas":"cctvAmericas",
  9.         "Children":"cctvChildren","Music":"cctvMusic","E":"cctvE","F":"cctvF",
  10.         "Arabic":"cctvAragic","Russian":"Russian","Gaoqing":"cctvGaoqing"}

  11. if __name__=="__main__":
  12.         if len(sys.argv)==1:
  13.                 Select="6"
  14.         else:
  15.                 if sys.argv[1] not in channel.keys():
  16.                         print "Out of Range. Please Select The following option:"
  17.                         keylist=channel.keys()
  18.                         keylist.sort()
  19.                         for i in keylist:
  20.                                 print  "%-9s: %-30s" %(i,channel[i])
  21.                         sys.exit(0)
  22.                 else:
  23.                         Select=sys.argv[1]
  24.         print '正在获取节目单,请稍后...'
  25.         date=strftime('%Y%m%d',localtime())
  26.         response = urllib.urlopen("http://bugu.cntv.cn/nettv/ibugu/28/"+channel[Select]+"/"+date+".shtml")
  27.         Result=response.read()
  28.         listP=re.findall(r"上午节目(.+?)<!--.subbar-->",Result,re.S)
  29.         list=re.findall(r"<table(.+?)</table>",listP[0],re.S)
  30.         list_AM=re.findall(r'<td width="19%">(.+?)</td>',list[0],re.S)
  31.         list_AM_prog=re.findall(r'<td width="66%">(.+?)</td>',list[0],re.S)
  32.         list_PM=re.findall(r"<td width='19%'>(.+?)</td>",list[1],re.S)
  33.         list_PM_prog=re.findall(r"<td width='66%'>(.+?)</td>",list[1],re.S)
  34.         #listnum=0
  35.         if len(list_AM)>len(list_PM):
  36.                 listnum=len(list_AM)
  37.         else:
  38.                 listnum=len(list_PM)
  39.         print "-"*80,
  40.         print " "*13+"上午节目"+" "*26+"下午节目"
  41.         print " "*14+"========"+" "*26+"========"
  42.         for i in range(listnum):
  43.                 if(i<len(list_AM)):
  44.                         print "%-4s %-29s" %(list_AM[i],list_AM_prog[i][:28]),
  45.                 else:
  46.                         print " "*35,
  47.                 if(i<len(list_PM)):
  48.                         print "%-4s %-30s" %(list_PM[i],list_PM_prog[i][:28])
  49.                 else:
  50.                         print " "*37
  51.         print "-"*80,
  52.         print " "*24,strftime("%Y年%m月%d日"),
  53.         print "%s 节目单" %channel[Select]

复制代码

tv.GIF (23.69 KB, 下载次数: 107)

tv.GIF

评分

参与人数 2可用积分 +5 收起 理由
nicozhou + 1 学习一下,呵呵
smallfish_xy + 4 原创内容

查看全部评分

论坛徽章:
0
2 [报告]
发表于 2009-08-10 08:35 |只看该作者
汗……差一点以为广告贴。
不错,支持此类小工具,感谢分享。

本来咱也有个脚本抓取一周电视预报,后来因为电视台更新太过频繁了就放弃了。

论坛徽章:
0
3 [报告]
发表于 2009-08-10 16:35 |只看该作者
好强!

论坛徽章:
0
4 [报告]
发表于 2009-08-10 16:39 |只看该作者
LZ以后能多发些这样的代码就好了,让我们这些小菜学习下

论坛徽章:
0
5 [报告]
发表于 2009-08-10 18:11 |只看该作者
  新手路过 。。。

论坛徽章:
0
6 [报告]
发表于 2009-08-10 20:45 |只看该作者
说明python有前途啊!

论坛徽章:
0
7 [报告]
发表于 2009-08-10 23:21 |只看该作者
正在获取节目单,请稍后...
Traceback (most recent call last):
  File "tv.py", line 26, in <module>
    list2=re.findall(r"<li>(.+?)</li>",list[0],re.S)
IndexError: list index out of range

论坛徽章:
0
8 [报告]
发表于 2009-08-10 23:59 |只看该作者

回复 #1 two 的帖子

自己修改了一下,3.0.1环境下可以运行,只是节目单排列太乱,搞不定了,请高手指点
#! /usr/bin/python
#  coding=utf-8
import urllib.request,re,unicodedata,string,sys
import locale
from time import strftime,localtime

channel={"1":"CCTV-1","2":"CCTV-2","3":"CCTV-3","4":"CCTV-4亚洲",
        "5":"CCTV-4欧洲","6":"CCTV-4meizhou","7":"CCTV-5","8":"CCTV-6",
        "9":"CCTV-7","10":"CCTV-8","11":"CCTV-9","12":"CCTV-10",
        "13":"CCTV-11","14":"CCTV-12","15":"CCTVxinwen","16":"CCTVshaoer",
        "17":"CCTVyinyue","18":"CCTV_E","19":"CCTV-F","20":"CCTV-gaoqing"}

if __name__=="__main__":
        if len(sys.argv)==1:
                Select="2"
        else:
                if string.atoi(sys.argv[1])>20 or string.atoi(sys.argv[1])<=0:
                        print("Out of Range. Please Select 1-20.")
                        sys.exit(0)
                else:
                        Select=sys.argv[1]
        print('正在获取节目单,请稍后...')
        date=strftime('%Y%m%d',localtime())
               response = urllib.request.urlopen("http://tv.cctv.com/soushi/28/0"+Select+"/"+date+".shtml")
        Result=response.read()
        Result=Result.decode('GBK')
        #list=re.findall(r"<div class='tlb_right'><div class='l'>(.+?)<script",Result,re.S)
        #list=re.findall(r"afternoon(.+?)<script",Result,re.S)#上午节目
        list=re.compile(r'上午节目(.+?)<script',re.S).findall(Result.strip())
        #list2=re.findall(r"<li>(.+?)</li>",list[0],re.S)
        list2=re.compile(r'<li>(.+?)</li>',re.S).findall(list[0].strip())
        morning=[]
        afternoon=[]
        listnum=0
        for i in range(len(list2)):
                i=re.sub('<.+?>','',list2)
                if locale.atoi(i[:2])>=12:  #将上午的节目于下午的节目分开
                        afternoon.append(i)
                else:
                        morning.append(i)
        if len(morning)>len(afternoon):
                listnum=len(morning)
        else:
                listnum=len(afternoon)
        print("-"*80,)
        print(" "*13+"上午节目"+" "*26+"下午节目")
        print(" "*13+"========"+" "*26+"========")
        for i in range(listnum):
                if(i<len(morning)):
                        print('{0:6s}|{1:15s}|'.format(morning[:5],morning[5:]),end="")
                else:
                        print(" "*26,end="")
                if(i<len(afternoon)):
                        #print('%-4s %-30s' %(afternoon[:5],afternoon[5:]))
                        print('{0:6s}{1:30s}'.format(afternoon[:5],afternoon[5:]))
                else:
                        print(" "*37)
        print("-"*80,)
        print(" "*24,strftime("%Y年%m月%d日"),end="")
        print("%s节目单" %channel[Select])

论坛徽章:
0
9 [报告]
发表于 2009-08-11 08:48 |只看该作者
呵呵,LZ真有意思。学习真是无处不在啊。

论坛徽章:
0
10 [报告]
发表于 2009-08-12 11:07 |只看该作者
在下不才,就楼主给出的案例做了个简要分析
http://blog.csdn.net/BillStone/archive/2009/08/11/4434999.aspx
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP