Python USES urllib module development of multi threaded douban small station mp3 download

  • 2020-04-02 13:23:11
  • OfStack


#! /usr/bin/python2.7
# -- coding:utf-8 --
import os, urllib,urllib2, thread,threading
import re
# Matching music url
reg=re.compile('{"name":"(.+?)".+?"rawUrl":"(.+?)",.+?}', re.I)

class downloader(threading.Thread):
        def __init__(self, url, name):
                threading.Thread.__init__(self)
                self.url=url
                self.name=name
        def run(self):
                print 'downloading from %s' % self.url
                urllib.urlretrieve(self.url, self.name)
threads=[]
# Multithreaded download file 
def main(url):
        response=urllib.urlopen(url)
        text=response.read()
        groups=re.finditer(reg, text)
        for g in groups:
                name=g.group(1).strip() + ".mp3"
                path=g.group(2).replace('\', '')
                t=downloader(path, name)
                threads.append(t)
                t.start()
                                                                                                                                                      
if __name__ == '__main__':
        main("http://site.douban.com/huazhou/")
        for t in threads:
                t.join()


Related articles: