Python uses the urllib module to develop a multi-thread Douban station mp3 download loader

Source: Internet
Author: User
Analyze the html of all mp3 (regular expression matching) file URLs on the Douban music website page, and then use urllib. urlretrieve to directly download the file to the local device. the code for downloading the file using multithreading technology is as follows:


#! /Usr/bin/python2.7
# -- Coding: UTF-8 --

Import OS, urllib, urllib2, thread, threading
Import re

# Match music URLs
Reg = re. compile ('{"name": "(. + ?) ". +? "RawUrl": "(. + ?) ",. + ?} ', Re. I)


Class downloader (threading. Thread ):
Def _ init _ (self, url, name ):
Threading. Thread. _ init _ (self)
Self. url = url
Self. name = name

Def run (self ):
Print 'downloading from % s' % self. url
Urllib. urlretrieve (self. url, self. name)

Threads = []

# Multi-threaded file download
Def main (url ):
Response = urllib. urlopen (url)
Text = response. read ()
Groups = re. finditer (reg, text)
For g in groups:
Name = g. group (1). strip () + ". mp3"
Path = g. group (2). replace ('\\','')
T = downloader (path, name)
Threads. append (t)
T. start ()


If _ name _ = '_ main __':
Main ("http://site.douban.com/huazhou ")
For t in threads:
T. join ()

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.