Last active
March 4, 2022 14:40
-
-
Save cporoske/a0723319e284d120d21b493065be942d to your computer and use it in GitHub Desktop.
Crawler for get the lrc of a song
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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