Download the Python code for the 8,000 nursery songs:
Copy the Code code as follows:
#-*-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 ". *
(.*)
. *downloadboxlist.*? 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)
You can refer to the changes if necessary.