网上有一些公开课视频教程还有课件啥的,手动下太慢了,写个python下载。我想尽可能的做到通用性,以后可以直接用的,代码如下,抛砖引玉,欢迎建议和意见:
import urllib.request
import re
import queue
import threading
import os
class download(threading.Thread):
def __init__(self,que):
threading.Thread.__init__(self)
self.que=que
def run(self):
while True:
if not self.que.empty():
print('-----%s------'%(self.name))
os.system('wget '+self.que.get())
else:
break
def startDown(url,rule,num,start,end,decoding=None):
if not decoding:
decoding='utf8'
req=urllib.request.urlopen(url)
body=req.read().decode(decoding)
rule=re.compile(rule)
link=rule.findall(body)
que=queue.Queue()
for l in link:
que.put(l[start:end])
for i in range(num):
d=download(que)
d.start()
if __name__=='__main__':
url='https://class.coursera.org/algo-004/lecture/index'
rule='
下面是我运行时的样子:
好了,下次要下载直接import这个文件就妥了~哈哈~希望得到大家批评,我也希望进步快点~
转载请注明:转自 http://blog.csdn.net/littlethunder/article/details/9396059