-1
これは、公開されているメソッドを使用して、musixmatch APIを使用してトラックの歌詞を取得します。トラックは、関連する歌詞がある場合、この形式でTypeErrorの取得:body []が空の場合、リストインデックスはstrではない整数またはスライスでなければなりません
json_obj = json.loads(raw.decode('utf-8'))
body = json_obj["message"]["body"]["lyrics"]["lyrics_body"]
プリント(json_obj)戻り:このメソッドは、この部分を有する
{'message': {'header': {'status_code': 200, 'execute_time': 0.0032379627227783}, 'body': {'lyrics': {'lyrics_id': 1290130, 'can_edit': 0, 'locked': 0, 'action_requested': '', 'verified': 0, 'restricted': 1, 'instrumental': 0, 'explicit': 0, 'lyrics_body': '', 'lyrics_language': 'en', 'lyrics_language_description': 'English', 'script_tracking_url': 'https://tracking.musixmatch.com/t1.0/m_js/e_1/sn_0/l_1290130/su_0/tr_3vUCAELvdbv2u68t6EhkMnd-8HjFVDR8ySJtVDvx1wu2t7bIHzQn4QjBnewHmPfaFosJFYkWv3dp_LyRwBhUrXjOsX9TA-28yzdsLebCvaG9shILvno8F94Mm6EuA6PvOiTKdFVL6hW2DSHd2uQQbOF-URNHli0NCOG2mnJS7i_pRvQM6jbzTs8GWGmfkz6SPG6SiRAw7OD1_ALUcvL6DWgqTDzzQyn1mxFzSyUWB5ddJTf3oTmgBoRKQKJJqiO6h2S6T40I73gio9TzqXUMeTjB7jKbL7pm-ma6s4zx-uAlRzVKvyEMxjJGks6p2CKu_NE3MZ26VcV4EVhaL0QcXsmVW49T064RtrZFc4qY9gqHoc_wreBVUPoftX1SWDPGzG4HZOiRXpj-V_YIvaLA2QAVsAVcBkwi9Zmb0onjsHTbknSmzO-6kcez0zrFc44LZOQ45of1eATGIVI/', 'pixel_tracking_url': 'https://tracking.musixmatch.com/t1.0/m_img/e_1/sn_0/l_1290130/su_0/tr_3vUCAAVtFUMcReVeKMMElPrsIdyf5IcOQyLMIduxSpy9U549RTNLfGorynAbS03DhldhcSFwWs_f9EiFdRepDw_HGQFZC6-hNtxts2M9eU1uD2Ga8RXdRq9pgm29WoAj8zcfH45WsO-tAfyA3oajqe-momfl2S4n51WgrBj4RxpmZ3DhrilGRO1Rhq5rqTyXRLG86fM7Hi0Bh2hXrXg115SiPywxjuMzLR2DMmGvu_Q--jjtFU38jTurZCsdtJ8slsdpvUzUo5Rs0EKdxboiopgPdTScdSQ-J3NRlXq8CFlK0j78ZMGo8KI9ZwdfMag5gPo5hI56PdcsAkfLkmvGjE2einZMruGrZVy08cZxAX7_ZdhXuLUY3RvleHog089sBJnzXDVqi7zb2yZSxrxYENQZuSR7KjR1fDN45I18ingQsAHXy18mJ6CUzWkrzwpV0-XAeNBe2d5mG9M/', 'html_tracking_url': 'https://tracking.musixmatch.com/t1.0/m_html/e_1/sn_0/l_1290130/su_0/tr_3vUCAEHn0YKFfsSIWi8uC0aMlSrJxMwAjYKks94IRU-wF13uNtElVESehNa14ZOa5h19bOKSN10QRhFF4V-Jwo6DLkoN5VVBVDpw9pqL5Ff0--pA2EUtqzvjZb3Z35J2V51RY9AFv6VYWgQ6Nm7ijSbRcbnjyqB3rvCbhUNqN_egIfyVPkDCjZZnyFIs5Vt8teA7zLE2ms5EDF6NphXfezUcsDZfN8hBtT8lvd_EjunDSKs7QaCMHmi8YV7aSiLWyFeQKBzKWm-YRq2z9kyXtcreXlGagr6UHazj88UTK_LO_TzT99BO4XKJwmQ1ARm8-c4nug9kxUic3EvDxS0CNclEXAuKdKpzZvE25PQZUb_dCyPNvCVHHV4C1XtlmOVXoRET6fuguSHlA7Hg7TqDo_PP8cbR-Q4_VeromQ4evewwzWrrNQQSA4VX0Z78Ll5nKF9lYVa5wWQTWi8/', 'lyrics_copyright': "Unfortunately we're not authorized to show these lyrics.", 'writer_list': [], 'publisher_list': [], 'backlink_url': 'https://www.musixmatch.com/lyrics/Bruce-Springsteen/No-Surrender?utm_source=application&utm_campaign=api&utm_medium=none%3A1409616623501', 'updated_time': '2010-06-29T03:43:49Z'}}}}
をしかし、トラックには歌詞がない場合には、本体と返します空:
{'message': {'header': {'status_code': 404, 'execute_time': 0.0011160373687744}, 'body': []}}
Traceback (most recent call last):
と体が空の場合、それが表示されます。
Traceback (most recent call last):
File "/Users/ozzy/PycharmProjects/semantic/semantic.py", line 240, in <module>
tracks[ID]['Lyric'] = song_lyric(title, tracks[ID]['Artist'])
File "/Users/ozzy/PycharmProjects/semantic/semantic.py", line 29, in song_lyric
body = json_obj["message"]["body"]["lyrics"]["lyrics_body"]
TypeError: list indices must be integers or slices, not str
を
が使用して、このイムを解決するには、次の
if json_obj["message"]["body"] != "":
print(json_obj)
body = json_obj["message"]["body"]["lyrics"]["lyrics_body"]
しかし、その作業はありませんが、私は同じ問題を取得します。なぜなのかご存知ですか?
完全な方法:
def song_lyric(song_name,artist_name):
while True:
querystring = apiurl_musixmatch + "matcher.lyrics.get?q_track=" + urllib.parse.quote(song_name) + "&q_artist=" + urllib.parse.quote(artist_name) +"&apikey=" + apikey_musixmatch + "&format=json&f_has_lyrics=1"
#matcher.lyrics.get?q_track=sexy%20and%20i%20know%20it&q_artist=lmfao
request = urllib.request.Request(querystring)
#request.add_header("Authorization", "Bearer " + client_access_token)
request.add_header("User-Agent", "curl/7.9.8 (i686-pc-linux-gnu) libcurl 7.9.8 (OpenSSL 0.9.6b) (ipv6 enabled)") #Must include user agent of some sort, otherwise 403 returned
while True:
try:
response = urllib.request.urlopen(request, timeout=4) #timeout set to 4 seconds; automatically retries if times out
raw = response.read()
#print(raw)
except socket.timeout:
print("Timeout raised and caught")
continue
break
json_obj = json.loads(raw.decode('utf-8'))
if json_obj["message"]["body"] != "":
print(json_obj)
body = json_obj["message"]["body"]["lyrics"]["lyrics_body"]
copyright = json_obj["message"]["body"]["lyrics"]["lyrics_copyright"]
tracking_url = json_obj["message"]["body"]["lyrics"]["html_tracking_url"]
if(tracking_url!= ""):
#print(body)
#print(tracking_url)
lyrics_tracking(tracking_url)
return (body + "\n\n" +copyright)
else:
return "None"
else:
return "None"
def lyrics_tracking(tracking_url):
while True:
querystring = tracking_url
request = urllib.request.Request(querystring)
#request.add_header("Authorization", "Bearer " + client_access_token)
request.add_header("User-Agent", "curl/7.9.8 (i686-pc-linux-gnu) libcurl 7.9.8 (OpenSSL 0.9.6b) (ipv6 enabled)") #Must include user agent of some sort, otherwise 403 returned
try:
response = urllib.request.urlopen(request, timeout=4) #timeout set to 4 seconds; automatically retries if times out
raw = response.read()
except socket.timeout:
print("Timeout raised and caught")
continue
break
print(raw)
'' ''(空の文字列)が '[]'(空のリスト)ではないためです。 '[] 'をテストするだけです。 – Julien