Python crawls to Baidu Music

Source: Internet
Author: User

Compiler environment: Python3.6


Code:

#!/usr/bin/env python#-*-coding=utf-8 -*-#AUTHOR:d uwentaoimport  requestsimport  Reimport jsondef get_sids_by_name (name):    url =  '/HTTP Music.baidu.com/search '     data = {          ' key ': Name    }    reponse = requests.get (url,params= Data)     reponse.encoding= ' Utf-8 '     html = reponse.text      #print   (HTML)     ul = re.findall (R ' <ul.*</ul > ',  html, re. S) [0]     #print (UL)     #  get sid sid&quot;:551560464     sids = re.findall (R ' sid&quot;:( \d+), ',  ul, re. S)     return sidsdef get_mp3_by_id (song_id):     song_id  = song_id     api = ' http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.song.play&format= jsonp&callback=jquery17205500581185420972_1513324047403&songid=%s&_=1513324048127 '  %  Song_id    response = requests.get (API)     data =  Response.text    data = re.findall (R ' \ ((. *) \) ',  data) [0]     data = json.loads (data)     title = data[' Songinfo ' [' title ']     mp3_url = data[' bitrate ' [' show_link ']    mp3_data  = requests.get (Mp3_url). Content    with open ('%s.mp3 '  % title, ' WB ')  as f:        f.write (Mp3_data)          print  ("Download Complete")         f.close () sid =  input ("Please enter the singer's name:") SIDS&NBsp;= get_sids_by_name (SID) for si in sids:    print  ("Downloading SID is", Si)     get_mp3_by_id (SI)



Execution Result:

Public Number:

Python crawls to Baidu Music

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.