import requests
import json
# initial message
message = "if i can\'t let it go out of my mind"
# split into list
split_message = message.split()
def decrementList(words):
for w in [words] + [words[:-x] for x in range(1,len(words))]:
url = 'http://ws.spotify.com/search/1/track.json?q='
request = requests.get(url + "%20".join(w))
json_dict = json.loads(request.content)
num_results = json_dict['info']['num_results']
if num_results > 0:
num_removed = len(words) - len(w)
#track_title = ' '.join(words)
track_title = "If I Can't Take It with Me"
for value in json_dict.items():
if value == track_title:
print "match found"
return num_removed, json_dict
num_words_removed, json_dict = decrementList(split_message)
在下面的代码中,我尝试将歌曲的名称与我的搜索查询相匹配。在此特定查询中,歌曲将不匹配,但我添加了一个与返回查询的歌曲匹配的变量。函数末尾的for循环应该与track标题变量匹配,但我无法弄清楚它为什么不起作用。有没有一种简单的方法来查找已知密钥的所有值?在这种情况下,键是“名称”
答案 0 :(得分:2)
您必须在tracks
词典中搜索曲目标题。所以,只需像这样更改你的代码
for value in json_dict["tracks"]:
if value["name"] == track_title:
它会打印
match found