foreword
What I will introduce to you today is that Python crawls audio data and saves it locally
development tools
Python version: 3.6.4
Related modules:
requests module
re module
os module
Environment build
Install Python and add it to the environment variable, and pip installs the required related modules.
Idea analysis
1. Page data
Open the page we want to crawl in the browser
Press F12 to enter the developer tool to see where the data we want is here
we need the page data
2. Code implementation
camouflage
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
}
url = 'https://music.163.com/discover/toplist?id=3778678'
1. Send request
response = requests.get(url=url, headers=headers)
2. Get data
print(response.text)
3. Parse the data
info_list = re.findall( 'lio<a href="/song \?id=(.*?)"">(.*?)</a></li>',html_data)
for music, title in result:
music_url = f'http://music.163.com/song/media/outer/url?id={
music}.mp3'
music_content = requests.get(url=music_url, headers=headers).content
4. Save data
with open(filename + title + '.mp3', mode='wb') as f:
f.write(music_content)
print(title)
Result display
at last
Today's sharing is over here, and interested friends can also try it.
If you have any questions about the article, or have other questions about python, you can leave a message in the comment area or private message me
If you think the article I shared is good, you can follow me, or give the article a thumbs up (/≧▽≦)/