当前位置:网站首页>Download Netease cloud music 10W + music library with Python

Download Netease cloud music 10W + music library with Python

2020-11-09 11:36:00 Watch the corner.

If you listen to music a lot , I can't get around Netease cloud , As a kind of emotional music App, I also like Netease cloud . Although it is now all 5G Times , We have more than enough mobile phone traffic , But playing online is not as reliable as storing music files locally , We'll use it today Python One click download Netease cloud music library .

First, look at the final effect .

Actually, it's not hard to download music , You just need to get the address of the music file, you can download it directly by reading the file stream . So the problem is how to get the playing address of music file .

List analysis

We can open the Netease cloud ranking list  https://music.163.com/#/discover/toplist?id=19723756, After careful analysis, we find that the left column of the page is full of leaderboards , Each list corresponds to a different list ID, Specifically ID How much is the , Open the developer tool directly to see .

From the picture above, we can see that the list is placed in a  class='f-cb'  Of  ul  In the list , So just get it  ul  List  li  Labels can be . And for each  li  The label is , Its  data-res-id  Property is the list id, And the list name belongs to the  li  Label under  div  in  class='name'  Of  p  Label under  a  Content of the label . So we got  li  After the collection of tags , Traverse the collection and take out the list in turn id And the list name .

So we have the following function , Get all the lists , The function returns a dictionary ,key by The list id, Value is the list name .

url = 'https://music.163.com/discover/toplist'
hd = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36'
}

def get_topic_ids():
    r = requests.get(url, headers=hd)
    html = etree.HTML(r.text)
    nodes = html.xpath("//ul[@class='f-cb']/li")
    logger.info('{}  {}'.format(' The list  ID', ' List name '))
    ans = dict()
    for node in nodes:
        id = node.xpath('./@data-res-id')[0]
        name = node.xpath("./div/p[@class='name']/a/text()")[0]
        ans[id] = name
        logger.info('{}  {}'.format(id, name))
    return ans

  

Song analysis

We've got all the list data above , So for a single list , I want to get all the songs under it .

The analysis page is known , The list of songs is in one  table  Medium , But by  requests.get(url,headers=hd)  Method to get the returned page text content , It seems that I can't get  table  Elemental . So we output its return value and do a careful analysis , Find that the song is in  class="f-hide"  Of  ul  In the label . Similar to getting a list , It's also necessary to get all of it first  li  label , Then get the songs one by one id And songs name That's all right. .

def get_topic_songs(topic_id, topic_name):
    params = {
        'id': topic_id
    }
    r = requests.get(url, params=params, headers=hd)
    html = etree.HTML(r.text)
    nodes = html.xpath("//ul[@class='f-hide']/li")
    ans = dict()
    logger.info('{}  The list  {}  There are songs in common  {}  The first  {}'.format('*' * 10, topic_name, len(nodes), '*' * 10))
    for node in nodes:
        id = node.xpath('./a/@href')[0].split('=')[1]
        name = node.xpath('./a/text()')[0]
        ans[id] = name
        logger.info('{}  {}'.format(id, name))

    return ans

  

Again, the function returns a dictionary ,key For the song id,value Name for the song .

Download songs

We also need a function to download songs , This function receives songs id, Then read it directly to the local as a file stream .

def down_song_by_song_id_name(id, name):
    if not os.path.exists(download_dir):
        os.mkdir(download_dir)
    url = 'http://music.163.com/song/media/outer/url?id={}.mp3'
    r = requests.get(url.format(id), headers=hd)
    is_fail = False
    try:
        with open(download_dir + name + '.mp3', 'wb') as f:
            f.write(r.content)
    except:
        is_fail = True
        logger.info("%s  Download error " % name)
    if (not is_fail):
        logger.info("%s  Download complete " % name)

  

Finally, all the operations are combined into main Function , As an entry function of a program .

def main():
    ids = get_topic_ids()
    while True:
        print('')
        logger.info(' Input  Q  Exit procedure ')
        logger.info(' Input  A  Download all the list songs ')
        logger.info(' Enter the list  Id  Download the current list songs ')

        id = input(' Please enter :')

        if str(id) == 'Q':
            break
        elif str(id) == 'A':
            for id in ids:
                down_song_by_topic_id(id, ids[id])
        else:
            print('')
            ans = get_topic_songs(id, ids[id])
            print('')
            logger.info(' Input  Q  Exit procedure ')
            logger.info(' Input  A  Download all songs ')
            logger.info(' Enter the song  Id  Download the current song ')
            id = input(' Please enter :')
            if str(id) == 'Q':
                break
            elif id == 'A':
                down_song_by_topic_id(id, ans[id])
            else:
                down_song_by_song_id_name(id, ans[id])

if __name__ == "__main__":
    main()

  

summary

Today, we use Netease cloud as the data source to download music files , The download operation is the simplest , The trouble is to analyze the list id And get the list of songs under the list , But the list of songs under the list is much more than 10 strip , And we get the function of the song  get_topic_songs  You can only get it at a time 10 Songs , This is because we are not in  headers  add to  cookie  As a result of , Because all the songs will be displayed only after logging in . Partners can log in to their accounts and add  cookie  Try it .

Be careful : If you're looking for python Well paid jobs . I suggest you write more about real enterprise projects and accumulate experience . Or you won't find a job , Of course, a lot of people have never been in a business , How can there be project experience ? So you have to find more enterprise projects and practice more . If you're lazy and don't want to find , You can also enter my Python Circle of communication :1156465813. There are some real enterprise project cases that I have written before in the group file . You can take it to study , If you don't understand, you can find me in your skirt , I'll answer you patiently when I have time .

 

The following is useless , For this blog to be crawled by search engines
(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)
python What is it Zero basis to learn python How long will it take? python Why is it called a reptile
python Novice reptile tutorial python Crawler universal code python How do reptiles make money
python Basic course Web crawler python python Classic examples of reptiles
python Reptiles
(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)
The above is useless , For this blog to be crawled by search engines

版权声明
本文为[Watch the corner.]所创,转载请带上原文链接,感谢