简体   繁体   English

Python-从musixmatch json响应中获取歌词

[英]Python - Getting lyrics back from musixmatch json response

In Python, I'm trying to parse the json response from musixmatch api but I'm having trouble figuring out how to access the information given, please help! 在Python中,我试图解析musixmatch api的json响应,但是在弄清楚如何访问给定信息时遇到困难,请帮忙! I can post the JSON Response later if necessary but it is quite a bit of text. 如有必要,我可以稍后发布JSON响应,但这是很多文本。

import json
from os.path json, dirname
from musixmatch import Musixmatch

musixmatch = Musixmatch(<api key>)

lyrics = musixmatch.matcher_lyrics_get('Grenade', 'Bruno Mars')


print (lyrics)

for item in jsonLyric['message']:
   for thing in item['body']:
     for stuff in thing['lyrics']:
        print(stuff['lyrics_body'])

This is the output that I'm getting back with the print(lyrics) 这是我返回的打印输出(歌词)

{"message": {"header": {"status_code": 200, "execute_time": 0.005511999130249}, "body": {"lyrics": {"lyrics_id": 14678126, "can_edit": 0, "locked": 1, "published_status": 3, "action_requested": "", "verified": 0, "restricted": 0, "instrumental": 0, "explicit": 0, "lyrics_body": "Easy come, easy go, that's just how you live oh\nTake, take, take it all, but you never give\nShould have known you was trouble from the first kiss\nHad your eyes wide open,\nWhy were they open?\n\nGave you all I had and you tossed it in the trash\nYou tossed it in the trash, you did\nTo give me all your love is all I ever asked 'cause\nWhat you don't understand is I'd catch a grenade for ya (yeah, yeah)\nThrow my head on a blade for ya (yeah, yeah)\nI'd jump in front of a train for ya (yeah, yeah)\nYou know I'd do anything for ya (yeah, yeah)\n\nOh oh, I would go through all this pain\nTake a bullet straight through my brain\nYes, I would die for ya baby\nBut you won't do the same\n\nNo, no, no, no\n...\n\n******* This Lyrics is NOT for Commercial use *******", "lyrics_language": "en", "lyrics_language_description": "English", "script_tracking_url": "https://tracking.musixmatch.com/t1.0/m_js/e_1/sn_0/l_14678126/su_0/rs_0/tr_3vUCALM0_W22leSuW0j51mdZjjfpE4x6a9jOCvWJRkoOBKyShPRRZQELOwGOsZtDZ8P7rI_Ugft_vMQta6YUky74d5X3RIpnkF1GkipbfBPqaDh2M9h4wtFKqwGgJoy8k52jf0kTKu2GFboxJpmcZP_TL17Tt5-rkmsIOB4VQk1LX_s66Du-rGVtu_NcuDuzquhrpHnIEyygjxJGcHYfbpHfFt3gDfDkAfuv25OHWoHt6FJarcoSeY5eloAlMJtqtQqXVIU4bVp3zmqnTdEsjj5mvyBOq2Yvl_WRsRYBiSP4fISEiW8Nz_cwKdLFqVo-9L4Pcz7ryASfVXfRD-nROD18LJIBomN5JzzcPMk9hEjHwmxt9PUccK5e_G23RYrgXgrWJlVQUyoIX5CNb_3IW893C0xfSrEm/", "pixel_tracking_url": "https://tracking.musixmatch.com/t1.0/m_img/e_1/sn_0/l_14678126/su_0/rs_0/tr_3vUCAB0Xj7CAyD4mjojoI5pSqo0L_Qojn7Q9jhh7eIMnru0DnnnxducKM9BTbyEX19XGYKgaLEjaRJUedry3aKHTl45sXeTudU6Y5g6DYg4UqxqUZVQuGT8RofGA25JeQfvfJnabBr4Ua2rDhu52CH9gY5T4MO58sTV_WEnz1OeovHSJJrcR7FJ6dGtZ7ddBSj5DAs3tXvKVZM-CMngsa4lVHgJvlKyQBI9dIlKpIasxUiHE8ESQ5nvq355A-9hB38ZO1xBSBE_poFKeFODVuxAYwt61nwkcy1INwNyRfu-88W0T8qdzmNdHPzC3CZ13W6_zvJQs144ir8AMF_ScjfXdyIZ5rBKT3yQYp4BIAuWYALrfv87mitBdkqukq_HiN79vaqsKTpn2dr5-IMe97ioMeI4xHwX2/", "html_tracking_url": "https://tracking.musixmatch.com/t1.0/m_html/e_1/sn_0/l_14678126/su_0/rs_0/tr_3vUCANQr9qsfeDL9iPgGrXIvJIBLLsgir3pFRzL4BslntB9GYllG57LNZuNC08A5g3r87JYVDgKTKk4QfYdU-j3sfs8i8onqzpCCV_w27R7ePS_k13wHNHBfSZpHQcJL4ocPj9i9E-Cms8yT9q4vLC31c3DGfudXXV6k5MajtdCVZdHaHfeNNm6tmstseeFCD6MuQxmRRkiUmHYTfgi2TfPwR4CzOZbEdULg5X6AHiiT4MMfIKAspZwghHUMhq3udos-8052sIMMUyB8nQrquajI8ZyZGX9UzKQi21ZDfyFz5fyUF9gYwzX4WA1gxhmVlsppG_-ocY7m29ddOUjEcg0HM0b7CsrJw8tAtcnwLnFovDcW-a7xzNUDlCkqXBLU_sDVg_na0uE4fH1QuYFKvwX5bwlQ1d-l/", "lyrics_copyright": "Lyrics powered by www.musixmatch.com. This Lyrics is NOT for Commercial use and only 30% of the lyrics are returned.", "writer_list": [], "publisher_list": [], "backlink_url": "https://www.musixmatch.com/lyrics/Bruno-Mars/Grenade?utm_source=application&utm_campaign=api&utm_medium=", "updated_time": "2016-03-18T14:38:56Z"}}}}

So as can be seen the JSON response is a dictionary, it has many other dictionaries inside of it but the one we want to reach it is the body dictionary which contains the "lyrics_body" and its value. 因此,可以看出JSON响应是一个字典,它内部还有许多其他字典,但我们要访问的是包含“ lyrics_body”及其值的body字典。

How could we do this? 我们该怎么做? Well it's actually quite simple. 嗯,这实际上很简单。 In one line we could just keep calling the dictionary and pass the key which contains another key, so for instance the first dictionary we want to access is the whole JSON response so we could call the lyrics variable, which contains the JSON response, and then we pass the key we want to access in this case "message", lyrics["message"]. 在一行中,我们可以继续调用字典并传递包含另一个键的键,例如,我们要访问的第一个字典是整个JSON响应,因此我们可以调用包含JSON响应的lyrics变量,然后在这种情况下,我们传递要访问的密钥“ message”,lyrics [“ message”]。

print(lyrics["message"]["body"]["lyrics"]["lyrics_body"])

The way you were doing it was also very simple. 您的操作方式也非常简单。 You just want to iterate through every single key in the integrated dictionaries. 您只想遍历集成词典中的每个键。

for message in lyrics:
    for body in lyrics[message]:
     if body == "body":
         for lyric in lyrics[message][body]:
             for lyrBod in lyrics[message][body][lyric]:
                 if lyrBod == "lyrics_body":
                     print(lyrics[message][body][lyric][lyrBod])

As you can see it's the same thing but just takes longer. 如您所见,这是一回事,只是需要更长的时间。

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM