如何利用python爬虫爬取音乐
利用python爬取想要听得音乐
找到相应的音乐网站
搜索相关音乐按F12查看当前页面
点击network,点击media
如果出现
上图着这个mp3文件,我们就可以python爬虫对这个链接进行下载
我们就可以得到我们想要的音乐了
下面是我的python爬取存储代码
with open(‘E:\歌曲\平凡之路.mp3’,‘wb’) as f:
f.write(r.content)
f.close()
print(‘ok’)
import requests
import pprint
import os
import pprint
def down(url,name):
r=requests.get(url)
with open(f’e:\music\{name}.mp3’,‘wb’) as f:
f.write(r.content)
f.close()
print(‘ok’)
data={
“songIds”:"" ,
“hq”: ,
“type”:“m4a,mp3” ,
“rate”:"",
“pt”: “0”,
“flag”: “-1”,
“s2p”: “-1”,
“prerate”: “-1”,
“bwt”: “-1”,
“dur”:" -1",
“bat”: “-1”,
“bp”: “-1”,
“pos”: “-1”,
“auto”: “-1”
}
response=requests.post(‘om/data/music/songlink’,data=data)
pprint.pprint(response.json()[‘data’][‘songList’][0])
for i in response.json()[‘data’][‘songList’]:
name=i[‘songName’]
print(name)
link=i[‘songLink’]
print(link)
down(link,name)