Skip to content

Instantly share code, notes, and snippets.

@scturtle scturtle/getsong.py
Created May 17, 2011

Embed
What would you like to do?
get song urls from baidu
# coding: utf-8
import sys,os,urllib2,xml
from xml.dom import minidom
lc = sys.getfilesystemencoding()
baseurl0="http://box.zhangmen.baidu.com/x?op=7&mode=1&count=1&listid=&title=%s"
baseurl1="http://box.zhangmen.baidu.com/x?op=12&count=1&mtype=4&title=%s"
song=raw_input('Song name:').decode(lc).encode('gbk')
data=urllib2.urlopen(baseurl0 % urllib2.quote(song))
data=data.read().replace('gb2312','utf-8').decode('gbk').encode('utf8')
doc=minidom.parseString(data) # 丫真挑剔
names=doc.getElementsByTagName('name')
count=0
for i in names:
print count,i.firstChild.nodeValue.replace('$',' ').encode(lc)
count+=1
choice=int(raw_input('\nyour choice:'))
data=urllib2.urlopen(baseurl1 % urllib2.quote(names[choice].firstChild.nodeValue.encode('gbk')))
data=data.read().replace('gb2312','utf-8').decode('gbk').encode('utf8')
doc=minidom.parseString(data)
urls=doc.getElementsByTagName('url')
print '\n song urls:'
for i in urls:
t=i.childNodes
pos=t[0].firstChild.nodeValue.rfind('/')
prefix=t[0].firstChild.nodeValue[0:pos+1]
suffix=t[1].firstChild.nodeValue#.split('?')[0]
print prefix+suffix
raw_input('\nenter to exit ...')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.