[转]Python下载百度新歌100的代码
2006-07-13 18:17
639 查看
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2006 UbuntuChina <http://www.ubuntu.org.cn>
# License: GPLv2
# Author: oneleaf <oneleaf AT gmail.com>
import httplib
import re
import urllib
import os
import locale
def getdownurl(url):
urllist=[]
conn = httplib.HTTPConnection('mp3.baidu.com')
conn.request("GET",url)
response = conn.getresponse()
html=response.read()
conn.close()
expression='http://220.181.27.54/m(.*)</a>'
listSentence = re.findall(expression, html)
lineno=0
while lineno<len(listSentence):
mp3url=re.search('title=(.*)onclick',listSentence[lineno])
if mp3url:
mp3url=mp3url.group(0)
mp3url=re.search('http(\S*)',mp3url)
if mp3url:
mp3url=mp3url.group(0)
try:
mp3url=mp3url.decode('gbk')
except:pass
urllist.append(mp3url)
lineno+=2
return urllist
def downmp3(url,author,name,filelist):
filename=author+"-"+name;
for i in filelist:
name=unicode(i,locale.getpreferredencoding())
if name.find(filename) == 0:
print u"文件已经下载,忽略。"
return 1
urllists=getdownurl(url)
for i in urllists:
print u"正在连接",i
ext=i[-4:]
try:
urlopen = urllib.URLopener()
fp=urlopen.open(i)
data = fp.read()
fp.close()
filename=filename+ext;
file=open(filename,'w+b')
file.write(data)
file.close()
print u"下载成功!"
return 1
except:
continue
return 0
if __name__ == "__main__":
conn = httplib.HTTPConnection('list.mp3.baidu.com')
conn.request("GET",'/list/newhits.html?id=1')
response = conn.getresponse()
html=response.read().decode('gbk')
conn.close()
expression='<a href="http://mp3.baidu.com/m(.*)</a>'
listSentence = re.findall(expression, html)
lineno=0
while lineno<len(listSentence):
url=re.search('(.*)target',listSentence[lineno])
url='/m'+url.group(0)[:-8]
name=re.search('blank>(.*)',listSentence[lineno])
name=name.group(0)[6:]
author=re.search('blank>(.*)',listSentence[lineno+1])
author=author.group(0)[6:]
print u"开始下载",author,name
filelist=os.listdir('.');
if downmp3(url,author,name,filelist)==0:
print u"下载",author,name,u'失败!'
lineno+=2
# -*- coding: utf-8 -*-
# Copyright (c) 2006 UbuntuChina <http://www.ubuntu.org.cn>
# License: GPLv2
# Author: oneleaf <oneleaf AT gmail.com>
import httplib
import re
import urllib
import os
import locale
def getdownurl(url):
urllist=[]
conn = httplib.HTTPConnection('mp3.baidu.com')
conn.request("GET",url)
response = conn.getresponse()
html=response.read()
conn.close()
expression='http://220.181.27.54/m(.*)</a>'
listSentence = re.findall(expression, html)
lineno=0
while lineno<len(listSentence):
mp3url=re.search('title=(.*)onclick',listSentence[lineno])
if mp3url:
mp3url=mp3url.group(0)
mp3url=re.search('http(\S*)',mp3url)
if mp3url:
mp3url=mp3url.group(0)
try:
mp3url=mp3url.decode('gbk')
except:pass
urllist.append(mp3url)
lineno+=2
return urllist
def downmp3(url,author,name,filelist):
filename=author+"-"+name;
for i in filelist:
name=unicode(i,locale.getpreferredencoding())
if name.find(filename) == 0:
print u"文件已经下载,忽略。"
return 1
urllists=getdownurl(url)
for i in urllists:
print u"正在连接",i
ext=i[-4:]
try:
urlopen = urllib.URLopener()
fp=urlopen.open(i)
data = fp.read()
fp.close()
filename=filename+ext;
file=open(filename,'w+b')
file.write(data)
file.close()
print u"下载成功!"
return 1
except:
continue
return 0
if __name__ == "__main__":
conn = httplib.HTTPConnection('list.mp3.baidu.com')
conn.request("GET",'/list/newhits.html?id=1')
response = conn.getresponse()
html=response.read().decode('gbk')
conn.close()
expression='<a href="http://mp3.baidu.com/m(.*)</a>'
listSentence = re.findall(expression, html)
lineno=0
while lineno<len(listSentence):
url=re.search('(.*)target',listSentence[lineno])
url='/m'+url.group(0)[:-8]
name=re.search('blank>(.*)',listSentence[lineno])
name=name.group(0)[6:]
author=re.search('blank>(.*)',listSentence[lineno+1])
author=author.group(0)[6:]
print u"开始下载",author,name
filelist=os.listdir('.');
if downmp3(url,author,name,filelist)==0:
print u"下载",author,name,u'失败!'
lineno+=2
相关文章推荐
- Python下载百度新歌100的代码
- python3抓取异步百度瀑布流动态图片(二)get、json下载代码讲解
- 面试题:100个小朋友围在一起,设定编号为1~100,依次按1,2,3,4,5,6,7,8,9循环报数,报到9的小朋友出圈,请写代码打印各个小朋友的出圈顺序?(Python实现)
- Python使用sftp实现上传和下载功能(实例代码)
- python3.3教程之模拟百度登陆代码分享
- 300行python代码的轻量级HTTPServer实现文件上传下载 推荐
- 三种Python下载url并保存文件的代码
- 用python实现的百度新歌榜、热歌榜下载器
- python模拟登录百度代码分享(获取百度贴吧等级)
- 40行python代码,搭建一个网站并实现用户登陆功能(附源码下载)
- python--实现1+..+100的代码集合
- 下载8000首儿歌的python代码
- 火星坐标、百度坐标、WGS84坐标转换代码(JS、python版)
- 使用Python下载歌词并嵌入歌曲文件中的实现代码
- python模拟登录百度代码分享(获取百度贴吧等级)
- python3.3教程之模拟百度登陆代码分享
- [转] 三种Python下载url并保存文件的代码
- python3下载远程代码并执行
- Python使用Socket(Https)Post登录百度的实现代码
- python ftp 按目录结构上传下载的实现代码