本文執行個體講述了Python實現提取Google音樂搜尋結果的方法。分享給大家供大家參考。具體如下:
Python的簡單指令碼,用於提取Google音樂搜尋頁面中的歌曲資訊,包括歌曲名,作者,專輯名,現在連結等,最多隻提取10頁結果。
#! /usr/bin/env python#coding=utf-8'''Created on 2011-8-19@author: yaoboyuan'''from urllib import request,parseimport re,sysdef extractSongRawData(text): '抓取每一首歌的未經處理資料' text = re.sub('\n+','',text) songList = re.findall('\',text) nums = len(songList) print('search ' + str(nums) + ' songs') return songListdef translate(text): '去掉text中的無用字元,轉換unicode碼' text = re.sub('\','',text) text = re.sub('\','',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 textdef extractSongName(song): '提取歌曲名字' td = re.findall('(?:\)',song) name = re.findall('.+?\(.*?)\',td[0]) authorName = name[0] authorName = translate(authorName) return authorNamedef extrackAlbumName(song): '提取專輯名字' td = re.findall('(?:\0: return td[0] else: return songdef extractLink(song): '提取歌曲下載連結' td = re.findall('''\