#! /usr/bin/python2.7
# -- coding:utf-8 --
import os, urllib, urllib2, thread, threading
import re
# 匹配音乐url里的id、、歌曲名字
reg = re.compile('<a target="_1" href="//www.greatytc.com/play/(.*?).htm" class="(.*?)">(.*?) </a>', 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 = []
# 多线程下载文件
def main(url):
response = urllib.urlopen(url)
text = response.read()
groups = re.finditer(reg, text)
for g in groups:
# print g.group(1) 这是id
# print g.group(3) 这是名字
name = g.group(3).strip() + ".m4a"
path = 'http://221.204.220.152/mp3.9ku.com/m4a/%s.m4a' % str(g.group(1))
t = downloader(path, name)
threads.append(t)
t.start()
if __name__ == '__main__':
main("http://www.9ku.com/music/bdhot.htm")
for t in threads:
t.join()
下载结果
可能有些歌曲404
那是因为九酷没有对应的歌曲会跳转到酷我音乐好像
最后我们的下载成果