python - 如何使用 iTune ssearch 服务构建 Python 程序

我是 Python 的新手,我被要求编写一个程序,询问用户搜索词,然后使用 iTunes 搜索服务对实体类型专辑执行搜索。然后程序应该打印返回了多少搜索结果。为每个结果打印艺术家姓名、专辑名称和曲目数。我应该使用 get() 函数和 json() 方法。

有人可以让我走上正确的道路吗?我对这个程序感到迷茫。

回答1

琳达,我认为我们在同一个 OpenSAP 课程中......这应该 get 你去。

import json
import requests as r

x = input("Please enter a search term: ")
url = f'https://itunes.apple.com/search?term={x}&entity=album' 
r = r.get(url) 

print(f"The search returned {r.text.count(x)} results."

回答2

import requests as r
Count = 0
artist = ""
album = ""
trackcount = 0
x = input("Please enter a search term: ")
url = f"https://itunes.apple.com/search?term={x}&entity=album"
f = r.get(url) 
d = f.json()
for x in d:
    if x == "resultCount":
        count = d["resultCount"]
        print(f"The search returned {str(count)} results.")
    elif x == "results":
        for lx in d["results"]:
            artist = lx["artistName"]
            album = lx["collectionName"]
            trackcount = lx["trackCount"]
            print(f"Artist: {artist} - Album: {album} - Track Count: {str(trackcount)}")

相似文章

随机推荐

最新文章