使用gevent多线程下载豆瓣音乐

感觉自己写的gevent下载音乐比迅雷快很多,话不多说,直接上代码。

 1 #! /usr/bin/python2.7

 2 # -- coding:utf-8 --

 3 

 4 import urllib,urllib2,gevent,re

 5 from gevent import monkey

 6 

 7 

 8 monkey.patch_all()

 9 

10 def worker(reg, url):

11     response=urllib.urlopen(url)

12     text=response.read()

13     groups=re.finditer(reg, text)

14     m_arr = []

15     for g in groups:

16         name=g.group(1).strip() + ".mp3"

17         path=g.group(2).replace('\\', '')

18         m_arr.append((name, path))

19     return m_arr

20 

21 def grun(path, name):

22     urllib.urlretrieve(path, name)

23                                                                                                                                                       

24 if __name__ == '__main__':

25     #匹配音乐url

26     reg=re.compile('{"name":"(.+?)".+?"rawUrl":"(.+?)",.+?}', re.I)    

27     musicArray = worker(reg, "http://site.douban.com/huazhou/")

28     jobs = []

29     for (name, path) in musicArray:

30         jobs.append(gevent.spawn(grun, path, name))

31     gevent.joinall(jobs)

你可能感兴趣的:(event)