直接上代码:
代码如下:
# -*- coding: utf-8 -*- import queue import threadingimport urllibimport urllib2import os
def down(url,n): print ‘item ‘+str(n)+’ start ‘ filename=urllib2.unquote(url).decode(‘utf8’).split(‘/’)[-1] urllib.urlretrieve(url, filename) print ‘item ‘+str(n)+’ finish ‘
def worker(): while true: i = q.get() url=i[0] n=i[1] down(url,n) q.task_done()
if __name__==”__main__”:
num_worker_threads=100
f=open(‘url.txt’) l=f.readlines() q = queue.queue() for i in range(num_worker_threads): t = threading.thread(target=worker) t.daemon = true t.start()
for i in range(0,len(l)): q.put((l[i],i))
q.join()