Python implementation download code sharing for 8,000 children's songs

Source: Internet
Author: User
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.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.