preface

Text and pictures from the network, only for learning, exchange, do not have any commercial purposes, copyright belongs to the original author, if you have any questions, please contact us to deal with.

PS: If you need Python learning materials, please click on the link below to obtain them

Note.youdao.com/noteshare?i…

Ximalaya FM is a professional audio sharing platform, collecting hundreds of millions of audio novels, audiobooks, audio books, children’s bedtime stories, crosstalk sketches, ghost stories and so on. Today we are going to learn how to capture audio of the Himalayas. Whenever and wherever I want.

Development environment:

  1. Anaconda5.2.0 (python3.6.5)

  2. Editor: PyCharm

Related modules:

import requests
import pprint
import reCopy the code

Implementation effect

The complete code

import requests
import pprint
import re
​
"""Batch download to find a pattern."""# block comment
"""Get the download address of the audio using the id of the audio passed in by the interface"""
​
headers = {
    'User-Agent': 'the Mozilla / 5.0 (Windows NT 10.0; Win64; X64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36'
}
​
​
def download_media(song_id, sond_name):
    # Select TAB shift+ TAB to undo indentation
    """According to songid name"""
    media_url = 'https://www.ximalaya.com/revision/play/v1/audio?id=' + song_id + '&ptype=1'
    # Fake the browser's identity
    response = requests.get(media_url, headers=headers)
    # I am requested by Python
    # print(response.request.headers)
    # dictionary type
    data = response.json()
    # print(data)
    # pprint.pprint(data)
    Print beautiful data as long as you know how to use it
    # pprint.pprint(data['data'])
    mp3_url = data['data'] ['src']
​
    Do video and audio image binaries need to be decoded?
    response = requests.get(mp3_url)
    # text Indicates the content of the text
    # print(response)
    with open(sond_name + '.m4a'.'wb') as f:
        f.write(response.content)
    print(sond_name, 'Download complete')
​
​
# sond_name = 'Sond_name'
# song_id = '98944395'
# download_media(song_id, sond_name)
for i in range(1, 32):
    response = requests.get('https://www.ximalaya.com/youshengshu/16411402/p'+str(i)+'/', headers=headers)
    # print(response.text)
    name_url = re.findall('
      
'
, response.text) for i in name_url: print(i[0], i[1]) download_media(i[1], i[0])Copy the code