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('\ ',text)
nums = len(songList)
print('search ' + str(nums) + ' songs')
return songList
def 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) )',song)
name = re.findall('.+?\(.*?)\',td[0])
songName = translate(name[0])
return songName
def extractAuthorName(song):
'提取作者名字'
td = re.findall('(?:\ )',song)
name = re.findall('.+?\(.*?)\',td[0])
authorName = name[0]
authorName = translate(authorName)
return authorName
def extrackAlbumName(song):
'提取专辑名字'
td = re.findall('(?:\ )',song)
name = re.findall('.+?\(.*?)\',td[0])
albumName = translate(name[0])
return albumName
def extractID(song):
'提取歌曲id'
td = re.findall('''\ 0:
return td[0]
else:
return song
def extractLink(song):
'提取歌曲下载链接'
td = re.findall('''\ ''',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 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
希望本文所述对大家的Python程序设计有所帮助。
查看更多关于Python实现提取谷歌音乐搜索结果的方法的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did89070