Python downloads 8000 children's songs and code sharing, and python8000 downloads

Source: Internet
Author: User

Python downloads 8000 children's songs and code sharing, and python8000 downloads

Download the python code for the first 8000 children's songs:

Copy codeThe 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 ". * 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.