Python實現提取谷歌音樂搜尋結果的方法

NO IMAGE

本文例項講述了Python實現提取谷歌音樂搜尋結果的方法。分享給大家供大家參考。具體如下:

Python的簡單指令碼,用於提取谷歌音樂搜尋頁面中的歌曲資訊,包括歌曲名,作者,專輯名,現在連結等,最多隻提取10頁結果。


#! /usr/bin/env python
#coding=utf-8
'''
Created on 2011-8-19
@author: yaoboyuan
'''
from urllib import request,parse
import re,sys
def extractSongRawData(text):
'抓取每一首歌的原始資料'
text = re.sub('\n ','',text)
songList = re.findall('\<tbody.*?\</tbody>',text)
nums = len(songList)
print('search '   str(nums)   ' songs')
return songList
def translate(text):
'去掉text中的無用字元,轉換unicode碼'
text = re.sub('\<b>','',text)
text = re.sub('\</b>','',text)
#find the 成 and translate into chinese
s = re.findall('&#([0-9] );',text)
if len(s)<=0:
return text
else:
text = ''
for i in range(len(s)):
value = int(s[i],10) #from str'123' to 10 base's int 124
text  = chr(value)  #from value to char
return text
def extractSongName(song):
'提取歌曲名字'
td = re.findall('(?:\<td class\="Title).*(?:\</td>)',song)
name = re.findall('. ?\<a. ?>(.*?)\</a>',td[0])
songName = translate(name[0])
return songName
def extractAuthorName(song):
'提取作者名字'
td = re.findall('(?:\<td class\="Artist).*(?:\</td>)',song)
name = re.findall('. ?\<a. ?>(.*?)\</a>',td[0])
authorName = name[0]
authorName = translate(authorName)
return authorName
def extrackAlbumName(song):
'提取專輯名字'
td = re.findall('(?:\<td class\="Album).*(?:\</td>)',song)
name = re.findall('. ?\<a. ?>(.*?)\</a>',td[0])
albumName = translate(name[0])
return albumName
def extractID(song):
'提取歌曲id'
td = re.findall('''\<tbody id\="([a-zA-Z0-9] )"''',song)
if len(td)>0:
return td[0]
else:
return song
def extractLink(song):
'提取歌曲下載連結'
td = re.findall('''\<td class\="Icon.*?(?=title\="下載").*?onclick\="(.*?)>''',song)
if len(td) == 0:
return 'NULL'
s = str(td[0])
rawLink = re.findall('http.*?(?=\?)',s)
if len(rawLink) == 0:
return s
link = rawLink[0]
link = re.sub('%3D','=',link)
id = extractID(song)
return link   '?id='   id
def extractPageNums(text):
'提取返回結果的頁數,最多要10頁'
pageList = re.findall('page_link',text)
return len(pageList)
def extractSongInfo(song):
'提取歌曲資訊,返回歌曲列表'
songList = []
for i in range(len(song)):
songName = extractSongName(song[i])
authorName = extractAuthorName(song[i])
albumName = extrackAlbumName(song[i])
link = extractLink(song[i])
songItem = [songName,authorName,albumName,link]
songList.append(songItem)
index = ''
if i<9:
index = '0'   str(i 1)
else:
index = str(i   1)
#print(index   ' '   songName   ' '   authorName   ' '   albumName   ' '   link)
return songList
def main():
while True:
url = 'http://www.google.cn/music/search?q='
key = input('請輸入歌曲名字或關鍵字:')
key = parse.quote(key) #統一編碼成utf-8
url  = key
mf = request.urlopen(url)
c = mf.readall()
c = str(c,encoding = 'utf-8')
num = extractPageNums(c)
print(str(num 1)   ' pages found')
song = extractSongRawData(c)
songList = extractSongInfo(song)
#if the result great than 2 pages, then request all pages
if num>0:
for i in range(num):
start = (i 1)*20
next_page = '&cat=song&start=%d'%(start)
#next_page = parse.quote(next_page) #統一編碼成utf-8
url  = next_page
mf = request.urlopen(url)
c = mf.readall()
c = str(c,encoding = 'utf-8')
song = extractSongRawData(c)
songList  = extractSongInfo(song) #find all results
for i in range(len(songList)): #print the result
index = ''
if i<9:
index = '0'   str(i 1)
else:
index = str(i   1)
print(index   ' '   str(songList[i]))
if __name__ == '__main__':
main()

希望本文所述對大家的Python程式設計有所幫助。

您可能感興趣的文章:

使用Python實現下載網易雲音樂的高清MVpython3音樂播放器簡單實現程式碼python中使用百度音樂搜尋的api下載指定歌曲的lrc歌詞python基於urllib實現按照百度音樂分類下載mp3的方法Python通過90行程式碼搭建一個音樂搜尋工具Python多執行緒結合佇列下載百度音樂的方法Python爬取網易雲音樂上評論火爆的歌曲聽歌識曲–用python實現一個音樂檢索器的功能Python爬取qq music中的音樂url及批量下載