Use Python to batch download music you like to listen to

Use Python to batch download music you like to listen to

The text and pictures of the article come from the Internet, and are for learning and communication purposes only. They do not have any commercial use. The copyright belongs to the original author. If you have any questions, please contact us for processing.

Knowledge points:

  1. requests
  2. Regular expression

Development environment:

  1. Version: anaconda5.2.0 (python3.6.5)
  2. Editor: pycharm

Third-party libraries:

  1. requests
  2. parsel

Web analytics

Analyze the real address of the music

Choose a song to take Chen Li's horse riding as an example

Open the developer tools, select network -> media -> refresh the webpage to get the real address of the music

However, the obtained address cannot be read when viewing the source code. It must be hidden by Baidu Music. There are generally two situations at this time. The first is to use JavaScript to splice or encrypt the request connection, and the second is to hide the data. Because we don’t know what happened. So we can only slowly analyze the requested data.

After analysis, we can see that the real music address exists in this API http://musicapi.taihe.com/v1/restserver/ting?method=baidu.ting.song.playAAC&format=jsonp&callback=jQuery17206453751179783578_1544942124991&songid=243093242&from=web&_= 1544942128336

And what we request this API to return is a json data (that is, the dictionary data type of python). As long as we use the rules of the dictionary, we can extract all our data.

url stitching to get all data

Earlier we got the real address of the music, and then we analyze the url of the real address, hoping to get the secret to downloading all the music.

A careful analysis of the url will reveal that the from parameter and _ after the? Do not affect the data request even if it does not exist.

And the songid in the following parameters is actually the unique id of the song, and the from parameter actually indicates which platform it came from.

So when we download music, as long as we get the songid of the song in batches, we can download all the songs.

Get singid in batch

Using developer tools, you can view the location of songid by viewing the source code of the webpage. If we analyze the URL of a singer page, you will find that the same can be constructed.

At this point, the entire web page analysis is over.

Achieve effect

Complete code

import re
import requests
def get_songid():
 """Get the songid of the music"""
 url ='http://music.taihe.com/artist/2517'
 response = requests.get(url=url)
 html = response.text
 sids = re.findall(r'href="/song/(\d+)"', html)
 return sids
def get_music_url(songid):
 """Get download link"""
 api_url = f'http://musicapi.taihe.com/v1/restserver/ting?method=baidu.ting.song.playAAC&format=jsonp&songid={songid}&from=web'
 response = requests.get(api_url.format(songid=songid))
 data = response.json()
 print(data)
 try:
 music_name = data['songinfo']['title']
 music_url = data['bitrate']['file_link']
 return music_name, music_url
 except Exception as e:
 print(e)
def download_music(music_name, music_url):
 """download music"""
 response = requests.get(music_url)
 content = response.content
 save_file(music_name+'.mp3', content)
def save_file(filename, content):
 """Save Music"""
 with open(file=filename, mode="wb") as f:
 f.write(content)
if __name__ == "__main__":
 for song_id in get_songid():
 music_name, music_url = get_music_url(song_id)
 download_music(music_name, music_url)

*Disclaimer: This article is organized on the Internet, and the copyright belongs to the original author. If the source information is wrong or infringes on rights, please contact me

ps: I recommend the python zero-base system learning exchange button I built qun: 937667509, there are free video tutorials in the group, development tools, e-books, project source code sharing. Learn python web, python crawler, data analysis, big data, artificial intelligence and other technologies if you don’t understand, you can join in to communicate and learn together, and make progress together!

Reference: https://cloud.tencent.com/developer/article/1538066 Use Python to batch download music you like to listen to-Cloud + Community-Tencent Cloud