Skip to content

Instantly share code, notes, and snippets.

@cporoske
Last active March 4, 2022 14:40
Show Gist options
  • Save cporoske/a0723319e284d120d21b493065be942d to your computer and use it in GitHub Desktop.
Save cporoske/a0723319e284d120d21b493065be942d to your computer and use it in GitHub Desktop.
Crawler for get the lrc of a song
import requests
import json
#基于网易云
#代码来自于开源项目musicbox,稍加修改
def search(s, stype=1, offset=0, total='true', limit=60):
action = 'http://music.163.com/api/search/get'
data = {
's': s,
'type': stype,
'offset': offset,
'total': total,
'limit': limit
}
return requests.post( action, data)
def get_lrc(music_id):
action = 'http://music.163.com/api/song/lyric?os=osx&id={}&lv=-1&kv=-1&tv=-1'.format( # NOQA
music_id)
try:
data = requests.get(action)
data = json.loads(data.text)
if 'lrc' in data and data['lrc']['lyric'] is not None:
lyric_info = data['lrc']['lyric']
else:
lyric_info = '未找到歌词'
return lyric_info
except requests.exceptions.RequestException as e:
return []
res = search("秋酿", limit = 1) #限制只获取一首歌
result = json.loads(res.text)
ids = result["result"]["songs"][0]["id"] # 获得歌曲id
lrc = get_lrc(ids) #返回歌词
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment