16 lines of python code to get sound effect material

Life is short, I use python

Sound asset:

Source code information e-book: Click here to jump to the business card at the end of the article

Required environment

Development environment

  • Python environment
  • Pycharm editor

Module

  • requests
  • re

Process explanation

First, we open the URL and right-click to select inspect


select network,
Refresh the page and slide down,
A page-4 and page-5 will appear.

A lot of data on these two pages is directly available here,
Let’s just find one and click play,
Then click on media,
There will be an audio file in the headers,
It is the download address I marked.

You can play directly or download directly


So how do you want to get this address?

We directly copy this string of numbers,
For example 32716,
Then click this search box in the upper left corner,
do a search.


After searching, we can see that page-5 has the sound link address of the audio.


Audio titles can also be found here

Then we click on headers and send a request directly to this url address.

First import the requests module

import requests

url is just the link

url = 'https://manually replace /search/word-/page-5'

Then we add a headers for camouflage

Just copy the contents of user-agent under headers directly here

headers = {<!-- -->
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36'
}

Then send the request, print it and see the result

response = requests. get(url=url, headers=headers)
print(response. text)

Too much to print out,
We search for MP3 directly on it,
precise positioning,
Its title is in the link below the mp3 file.


Then we copy it over,
Use regular expressions to match the content in the middle,
The middle url is replaced with (.*?).

First import the re module

import re

Just copied the content, .*? enclosed in parentheses.

To match from response.text, the matched content is received by the variable play_url_list.

play_url_list = re.findall('<div class="ui360 ui360-vis"><a href="(.*?)"></a></div>', response .text)

Then print it to see if there is any matching content

print(play_url_list)

You can see that the mp3 file is directly matched,
It is contained in a list.


Then we also need its title name, and copy it in the same way.


Still the same operation, url and name are replaced with .*?

To match from response.text, the matched content is received by the variable name_list.

for play_url, name in zip(play_url_list, name_list):
    mp3_content = requests.get(url=play_url, headers=headers).content

Then save directly,
with open give it a folder name,
add name,
Add the suffix of .mp3,
Save mode mode = wb ,
Use the variable f.write to receive mp3_content

 with open('sound effect\' + name + '.mp3', mode='wb') as f:
        f.write(mp3_content)

Here we did not write to automatically create folders,
So you need to manually create the folder,
Then put your name into it


Let’s print it to see the result

print(name)


The relevant data content is saved in the folder you created

Note: Please manually replace all the urls by yourself, I will delete them here, otherwise they will not be approved

import requests
import re
url = 'https://Here, please replace /search/word-/page-5'
headers = {<!-- -->
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36'
}
response = requests. get(url=url, headers=headers)

play_url_list = re.findall('
', response.text) name_list = re.findall('.*?', response.text) print(play_url_list) print(name_list) for play_url, name in zip(play_url_list, name_list): mp3_content = requests.get(url=play_url, headers=headers).content with open('sound effect\' + name + '.mp3', mode='wb') as f: f.write(mp3_content) print(name)


Questions & Answers · Source Code Acquisition · Technical Exchange · Group learning please contact