#!/usr/bin/env python
# coding=utf-8 import urllib
import urllib2
import cookielib def setcookie(enable_proxy=False):
proxy_handler = urllib2.ProxyHandler({"http":'10.13.61.118:6666', "https":'10.13.61.118:6666'}) # 学校代理
cookiejar = cookielib.LWPCookieJar()
cookie_support = urllib2.HTTPCookieProcessor(cookiejar)
debug_hander = urllib2.HTTPHandler(debuglevel=1) # debuglevel=0
if enable_proxy:
opener = urllib2.build_opener(cookie_support, proxy_handler, debug_hander)
else:
opener = urllib2.build_opener(cookie_support, debug_hander)
urllib2.install_opener(opener) def download(fp,tofile):
outf = open(tofile,'wb')
c = 0
print ('Downloading to %s'%(tofile))
while True:
s = fp.read(1024*128)
if len(s) == 0:
break
outf.write(s)
c += len(s)
print ('Download %d'%(c))
outf.close() import os
def main(num=32, urlhost='https://class.coursera.org/ml-006/lecture/download.mp4?lecture_id='):
for i in xrange(1,num+1):
if os.path.exists(str(i)+'.mp4'):continue
print urlhost+str(i) header = {
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0',
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language':'zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3',
'Accept-Encoding':'gzip, deflate, sdcn',
}
req = urllib2.Request(url=urlhost+str(i), headers=header)
try:
fp = urllib2.urlopen(req)
except:
continue
download(fp, str(i)+'.mp4')
fp.close() if __name__ == '__main__':
setcookie()
main(106, 'https://class.coursera.org/pgm/lecture/download.mp4?lecture_id=')
05-11 20:19