脚本专栏 
首页 > 脚本专栏 > 浏览文章

Python实现提取谷歌音乐搜索结果的方法

(编辑:jimmy 日期: 2024/11/29 浏览:3 次 )

本文实例讲述了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.*"Title).*("Artist).*("Album).*("([a-zA-Z0-9]+)"''',song)
  if len(td)>0:
    return td[0]
  else:
    return song
def extractLink(song):
  '提取歌曲下载链接'
  td = re.findall('''\<td class\="Icon.*"下载").*"(.*?)>''',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自动登录126邮箱的方法
下一篇:Python获取邮件地址的方法
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 网站地图 SiteMap