#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])
本文介绍了一个使用Python编写的简单网易视频课程下载器。该下载器利用BeautifulSoup解析网页,通过wget下载视频文件。它首先从课程页面抓取课程列表,然后针对每个课程下载对应的FLV视频文件。
1万+

被折叠的 条评论
为什么被折叠?



