[转]Python下载百度新歌100的代码#!/usr/bin/python 
[转]Python下载百度新歌100的代码#
 -*- coding: utf-8 -*- 
[转]Python下载百度新歌100的代码#
 Copyright (c) 2006 UbuntuChina <http://www.ubuntu.org.cn> 
[转]Python下载百度新歌100的代码#
 License: GPLv2 
[转]Python下载百度新歌100的代码#
 Author: oneleaf <oneleaf AT gmail.com> 
[转]Python下载百度新歌100的代码

[转]Python下载百度新歌100的代码
import httplib 
[转]Python下载百度新歌100的代码
import re 
[转]Python下载百度新歌100的代码
import urllib 
[转]Python下载百度新歌100的代码
import os 
[转]Python下载百度新歌100的代码
import locale 
[转]Python下载百度新歌100的代码
[转]Python下载百度新歌100的代码
def getdownurl(url): 
[转]Python下载百度新歌100的代码    urllist
=[] 
[转]Python下载百度新歌100的代码    conn 
= httplib.HTTPConnection('mp3.baidu.com'
[转]Python下载百度新歌100的代码    conn.request(
"GET",url) 
[转]Python下载百度新歌100的代码    response 
= conn.getresponse() 
[转]Python下载百度新歌100的代码    html
=response.read() 
[转]Python下载百度新歌100的代码    conn.close() 
[转]Python下载百度新歌100的代码    expression
='http://220.181.27.54/m(.*)</a>' 
[转]Python下载百度新歌100的代码    listSentence 
= re.findall(expression, html) 
[转]Python下载百度新歌100的代码    lineno
=
[转]Python下载百度新歌100的代码    
while lineno<len(listSentence): 
[转]Python下载百度新歌100的代码        mp3url
=re.search('title=(.*)onclick',listSentence[lineno]) 
[转]Python下载百度新歌100的代码        
if mp3url: 
[转]Python下载百度新歌100的代码           mp3url
=mp3url.group(0) 
[转]Python下载百度新歌100的代码           mp3url
=re.search('http(\S*)',mp3url) 
[转]Python下载百度新歌100的代码           
if mp3url: 
[转]Python下载百度新歌100的代码              mp3url
=mp3url.group(0) 
[转]Python下载百度新歌100的代码              
try
[转]Python下载百度新歌100的代码                  mp3url
=mp3url.decode('gbk'
[转]Python下载百度新歌100的代码              
except:pass 
[转]Python下载百度新歌100的代码              urllist.append(mp3url) 
[转]Python下载百度新歌100的代码        lineno
+=2 
[转]Python下载百度新歌100的代码    
return urllist 
[转]Python下载百度新歌100的代码
[转]Python下载百度新歌100的代码
def downmp3(url,author,name,filelist): 
[转]Python下载百度新歌100的代码    filename
=author+"-"+name; 
[转]Python下载百度新歌100的代码    
for i in filelist: 
[转]Python下载百度新歌100的代码        name
=unicode(i,locale.getpreferredencoding()) 
[转]Python下载百度新歌100的代码        
if name.find(filename) == 0: 
[转]Python下载百度新歌100的代码            
print u"文件已经下载,忽略。" 
[转]Python下载百度新歌100的代码            
return 1 
[转]Python下载百度新歌100的代码    urllists
=getdownurl(url) 
[转]Python下载百度新歌100的代码    
for i in urllists:        
[转]Python下载百度新歌100的代码        
print u"正在连接",i 
[转]Python下载百度新歌100的代码        
[转]Python下载百度新歌100的代码        ext
=i[-4:] 
[转]Python下载百度新歌100的代码        
try
[转]Python下载百度新歌100的代码            urlopen 
= urllib.URLopener() 
[转]Python下载百度新歌100的代码            fp
=urlopen.open(i) 
[转]Python下载百度新歌100的代码            data 
= fp.read() 
[转]Python下载百度新歌100的代码            fp.close() 
[转]Python下载百度新歌100的代码            filename
=filename+ext; 
[转]Python下载百度新歌100的代码            file
=open(filename,'w+b'
[转]Python下载百度新歌100的代码            file.write(data) 
[转]Python下载百度新歌100的代码            file.close() 
[转]Python下载百度新歌100的代码            
print u"下载成功!" 
[转]Python下载百度新歌100的代码            
return 1 
[转]Python下载百度新歌100的代码        
except
[转]Python下载百度新歌100的代码            
continue 
[转]Python下载百度新歌100的代码    
return 0 
[转]Python下载百度新歌100的代码
[转]Python下载百度新歌100的代码
if __name__ == "__main__"
[转]Python下载百度新歌100的代码    conn 
= httplib.HTTPConnection('list.mp3.baidu.com'
[转]Python下载百度新歌100的代码    conn.request(
"GET",'/list/newhits.html?id=1'
[转]Python下载百度新歌100的代码    response 
= conn.getresponse() 
[转]Python下载百度新歌100的代码    html
=response.read().decode('gbk'
[转]Python下载百度新歌100的代码    conn.close() 
[转]Python下载百度新歌100的代码    expression
='<a href="http://mp3.baidu.com/m(.*)</a>' 
[转]Python下载百度新歌100的代码    listSentence 
= re.findall(expression, html) 
[转]Python下载百度新歌100的代码    lineno
=
[转]Python下载百度新歌100的代码    
while lineno<len(listSentence): 
[转]Python下载百度新歌100的代码       url
=re.search('(.*)target',listSentence[lineno]) 
[转]Python下载百度新歌100的代码       url
='/m'+url.group(0)[:-8
[转]Python下载百度新歌100的代码       name
=re.search('blank>(.*)',listSentence[lineno]) 
[转]Python下载百度新歌100的代码       name
=name.group(0)[6:] 
[转]Python下载百度新歌100的代码       author
=re.search('blank>(.*)',listSentence[lineno+1]) 
[转]Python下载百度新歌100的代码       author
=author.group(0)[6:] 
[转]Python下载百度新歌100的代码       
print u"开始下载",author,name 
[转]Python下载百度新歌100的代码       filelist
=os.listdir('.'); 
[转]Python下载百度新歌100的代码       
if downmp3(url,author,name,filelist)==0: 
[转]Python下载百度新歌100的代码          
print u"下载",author,name,u'失败!' 
[转]Python下载百度新歌100的代码       lineno
+=2 
[转]Python下载百度新歌100的代码

相关文章:

  • 2021-10-15
  • 2021-11-25
  • 2021-11-04
  • 2022-12-23
  • 2022-01-14
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-02-04
  • 2022-12-23
  • 2021-12-03
  • 2022-01-01
  • 2021-06-07
  • 2021-08-30
  • 2021-07-15
相关资源
相似解决方案