Download 8000 children's songs in Python-Python tutorial

Source: Internet
Author: User
This article mainly introduces how to download the code for the first 8000 children's songs in Python. This article provides the implementation code directly and downloads resources from the companion network, for more information, see the python code for downloading the 8000 children's song:

The code is 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 t:
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('{downloadbox ')
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 t:
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)

If necessary, refer to continue modification.

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.