#**********python3.3********** import urllib.request import re import time import gzip patt1 = '(((\\\\/)\w+)+\.wma)' #获取歌曲路径的正则 patt2 = 'href="([^"]+)"\starget="_1ting">([^<]+).*\s+.*\s+.*\s+[^>]+>([^<]+).*\s+.*\s+.*\s+[^>]+>([^<]+)' #获取歌曲的名字,歌手,专辑的正则 url = 'http://so.1ting.com/song?q='#搜索的路径 songurl = 'http://nonie.1ting.com:9092'#歌曲路径的前半部分 while True: songname = input("请输入歌曲名字(按0退出):") if (songname == '0' ): time.sleep(1) print("3秒后退出") time.sleep(1) print("2秒后退出") time.sleep(1) print("1秒后退出") exit() changename = urllib.parse.quote(url+songname,':?=/') req = urllib.request.Request(changename) request = urllib.request.urlopen(req) html = request.read() templist = re.findall(patt2,html.decode('utf8')) i = 0 for item in templist: print(str(i)+' '+item[1]+":"+item[2]+":"+item[3]) i+=1 selectone = int(input('请输入序号选择一首下载:')) url1 = templist[selectone][0]#所选择的网址 req1 = urllib.request.Request(url1) request1 = urllib.request.urlopen(req1) if ((request1.info().get("Content-Encoding")) == 'gzip'): html1 = gzip.decompress(request1.read()) else: html1 = request1.read() songpath = re.findall(patt1,html1.decode('utf8','ignore')) finall = songurl+songpath[0][0].replace("\\",'') print('正在下载......') f = urllib.request.urlopen(finall) data = f.read() with open(songname+'_'+templist[selectone][2]+'.wma','wb') as code: code.write(data)#将歌曲存储在同一文件家下 print('下载完成......')