下载8000首儿歌的python的代码:
复制代码 代码如下:
#-*- coding: UTF-8 -*-
from pyquery import PyQuery as py
from lxml import etree
import urllib
import re
import os
import sys
import logging
def format(filename):
tuple=(' ',''','\'')
for char in tuple:
if (filename.find(char)!=-1):
filename=filename.replace(char,"_")
return filename
def download_mp3(mp3_url, filename,dir):
f = dir+"\\"+filename
if os.path.exists(f):
logger.debug(f+" is existed.")
return
try:
open(f, 'wb').write(urllib.urlopen(mp3_url).read())
logger.debug( filename + ' is downloaded.')
except:
logger.debug( filename + ' is not downloaded.')
def download_all_mp3(start,end,dir,logger):
for x in range(start,end):
try:
url = "http://www.youban.com/mp3-d" + str(x) + ".html"
logger.debug(str(x) + ": "+url)
doc = py(url=url)
e = doc('.mp3downloadbox')
if e is None or e == '':
logger.debug(url+" is not existed.")
return
e = unicode(e)
#logger.debug( e)
regex = re.compile(ur".*<h1>(.*)</h1>.*downloadboxlist.*"(.*"",re.UNICODE|re.S)
m = regex.search(e)
if m is not None:
title = m.group(1).strip()
title2 = str(x)+"_"+title + ".mp3"
#title2 = re.sub(' ','_',title2)
title2 = format(title2)
link = m.group(2)
#logger.debug( "title:" + title + " link:" + link)
if link == '' or title == '':
logger.debug(url + " is not useful")
continue
logger.debug(str(x)+": "+link)
download_mp3(link,title2,dir)
except:
logger.debug(url+" met exception.")
continue
if __name__ == "__main__":
dir_root = "e:\\song"
if sys.argv[3] != '': dir_root=sys.argv[3]
start,end = 1,8000
if sys.argv[1] >= 0 and sys.argv[2]>=0:
start,end = int(sys.argv[1]),int(sys.argv[2])
print ("Download from %s to %s.\n" % (start,end))
dir = dir_root + "\\"+str(start)+"-"+str(end)
if not os.path.exists(dir):
os.mkdir(dir)
print "Download to " + dir + ".\n"
logger = logging.getLogger("simple")
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler(dir+"\\"+"download.log")
ch = logging.StreamHandler()
formatter = logging.Formatter("%(message)s")
ch.setFormatter(formatter)
fh.setFormatter(formatter)
logger.addHandler(ch)
logger.addHandler(fh)
download_all_mp3(start,end,dir,logger)
有需要的可以参考继续修改。
Python,下载儿歌
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
更新日志
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]