Chinaunix首页 | 论坛 | 博客
  • 博客访问: 205256
  • 博文数量: 48
  • 博客积分: 1935
  • 博客等级: 上尉
  • 技术积分: 491
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-29 00:59
文章分类

全部博文(48)

文章存档

2011年(1)

2010年(47)

我的朋友

分类: Python/Ruby

2010-09-21 00:42:02

python下载songtaste歌曲(终端版本)
2009-09-10 11:49

前几天说做个下载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','replace')
            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

阅读(1036) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~