#coding:gbk
import os, sys, urllib
from BeautifulSoup import BeautifulSoup
true = True
false = False
null = None
def output(str):
"""
输出调试信息
"""
print str
def download_file(url, filename):
print url
print filename
output('download %s, save to %s ...' % (url, filename))
if (os.path.exists(filename)):
return 0
cmd = 'wget -O %s %s' % (filename, url)
output(cmd.encode('gbk'))
return os.system(cmd.encode('gbk'))
def download_course(url, number, title):
course_id = url[url.rfind('/')+1:].split('.')[0]
dirletters = course_id[-2:]
xmlurl = 'http://live.ws.126.net/movie/%s/%s/2_%s.xml' % (dirletters[0], dirletters[1], course_id)
#output(xmlurl)
content = urllib.urlopen(xmlurl).read()
soup = BeautifulSoup(content)
flvurl = soup.find('flv').text
download_file(flvurl, '%s_%s.flv' % (str(number).rjust(3,'0'), title))
return
def download_163v_video(url):
content = urllib.urlopen(url).read()
soup = BeautifulSoup(content)
items = soup.findAll('li', {'class':'odd_li'})
courses = [(item.a['href'], item.h3.a.text) for item in items]
index = 0
for (url, title) in courses:
index += 1
download_course(url, index, title)
return
if __name__ == '__main__':
download_163v_video(sys.argv[1])