python抓取1ting音樂網站的歌曲

#**********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('下載完成......')
相關文章
相關標籤/搜索