This article mainly introduces Python's implementation code for obtaining the CCTV program list. It involves Python page collection techniques and has some reference value, for more information, see the following example. Share it with you for your reference. The details are as follows:
#! /Usr/bin/python #-*-coding: UTF-8-*-import urllib, re, unicodedata, string, sys from time import strftime, localtime channel = {"1 ": "CCTV-1", "2": "CCTV-2", "3": "CCTV-3", "4": "CCTV-4 Asia", "5": "CCTV-4 Europe ", "6": "CCTV-4 America", "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 ": "CCTV News", "16": "CCTV children", "17": "CCTV music", "18": "CCTV_E", "19": "CCTV-F ", "20": "CCTV-HD"} if _ name __= = "_ main __": print "@" print "@ you can enter a number (1-20) after the command line) to select the channel "print" @ get the channel list "print" @ "if len (sys. argv) = 1: Select = "8" else: if sys. argv [1] = "help": for I in range (len (channel): print "% 3d: % 11 s" % (I + 1, channel ["% s" % (I + 1)]), if (I % 4 = 3): print "" sys. exit (0) 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 'is obtaining the program list. Please wait... 'date = strftime ('% Y % m % d', localtime () response = urllib. urlopen (" http://tv.cctv.com/soushi/28/0 "+ Select +"/"+ date +". shtml ") Result = response. read () # list = re. findall (r"(. + ?)
(. + ?)", List [0], re. s) morning = [] afternoon = [] listnum = 0 for I in range (len (list2): I = re. sub ('<. +?> ', '', List2 [I]) if string. atoi (I [: 2])> = 12: # Separate the morning program from the afternoon program afternoon. append (I) else: morning. append (I) if len (morning)> len (afternoon): listnum = len (morning) else: listnum = len (afternoon) print "-" * 80, print "" * 13 + "morning program" + "" * 26 + "Afternoon Program" print "" * 14 + "======" + ""* 26 + "========" for I in range (listnum): if (I
I hope this article will help you with Python programming.