分类: Python/Ruby
2010-09-21 00:42:02
前几天说做个下载songtaste上歌曲的脚本试试 不过在保存文件的时候总是出现乱码的错误 不过终于解决
#!/usr/bin/python
#-*-coding:gbk-*-
#在linux下把第二行换成#-*-coding:utf-8-*-
import re
import urllib
import urlparse
import httplib
import time
import progressbar
class GetSong():
def __init__(self,songname,netnum):
self.songname=songname
self.netnum=netnum
def parser(self):
for i in range(len(netnum)):
url=urllib.URLopener()
url=url.open(self.netnum[i])
data=url.read()
m=re.compile(r'(?<=id="playicon"...).*?(?=>
s=[]
s=m.findall(data)
for ss in s:
ss=ss.replace('\'','')
ss=ss.replace('"','')
x=ss
neis=x.split(',')
html=''+neis[2]
html=html.replace(' ','')
songname1=self.songname[i] #在linux下换成songname1=self.songname[i].decode('gb18030',
html=html+'.mp3'
print self.songname[i]
try:
url1=urllib.urlopen(html)
except:
print '不能下载'
continue
parsedurl=urlparse.urlparse(html)
httpcon=httplib.HTTPConnection(parsedurl[1])
try:
httpcon.request('GET', parsedurl[2])
except:
print '歌曲不能下载或者需要手动下载'
continue
f=open(songname1,'w+b')
reponse=httpcon.getresponse()
if reponse.status==200:
size=reponse.getheader('Content-Length')
httpcon.close()
url=urllib.URLopener()
url=url.open(html)
x=0
par=progressbar.ProgressBar().start()
while True:
line =url.readline()
x=x+len(line)
f.write(line)
par.update(x*100/int(size))
if len(line)==0:
break
par.finish()
print songname[i],'下载完了'
'''
解析第一次获得的网页的内容
'''
m=re.compile(r'(?<=
wangzhi=raw_input()
f=urllib.urlopen(wangzhi)
data=f.read()
s=[]
s=m.findall(data)
wl=s[0]
wl=wl.replace('\n\nWL','')
wl=wl.replace('\t\n