文档为here
我是使用api的新手,我一直试图弄清楚如何使用它而没有运气。我已经拥有客户端ID,密码ID和访问令牌。
我要做的是:
-get artist id
-get all the songs by an artist
-get the lyrics to a song
我现在没有任何代码,因为我无法弄清楚如何调用api除了
import requests
genius_client_id = ''
genius_secret_id = ''
genius_client_access_token = ''
base_url = 'https://api.genius.com/'
r = requests.get(*insert api call here*)
print(r)
任何指导都将不胜感激。感谢。
修改
我现在正在使用此功能 - 输入艺术家姓名和歌曲,它将返回data。现在我将如何查看这些数据 - 比如我如何获得'full_title','id'或歌词?
#search for song
import requests
client_access_token = ''
base_url = 'https://api.genius.com'
user_input = input('artist and song: ').replace(" ", "-")
path = 'search/'
request_uri = '/'.join([base_url, path])
print(request_uri + user_input)
params = {'q': user_input}
token = 'Bearer {}'.format(client_access_token)
headers = {'Authorization': token}
r = requests.get(request_uri, params=params, headers=headers)
print(r.text)
答案 0 :(得分:0)
看起来你非常接近。我只是快速看一下,但打赌你需要这样的东西。
import requests
payload={
'genius_client_id' : '',
'genius_secret_id' : '',
'genius_client_access_token' : ''}
base_url = 'https://api.genius.com/'
r = requests.get(base_url, params=payload)
print(r.status_code) #200 is good
P.S。请求文档非常有用,也可以看看那里
答案 1 :(得分:0)
您即将获得歌曲的详细信息(歌词,id等)。您要做的就是遍历JSON元素。 JSON文件包含有关搜索的信息。 在这种情况下,该文件将包含有关搜索中出现的许多歌曲的数据,但我将展示如何导航到第一首歌曲的歌词。
#this gives the link for the lyrics page of the searched song.
#This is for the first result of the search. You can use a for loop if you want the URL for more results.
URL = json['response']['hits'][0]['result']['url']
print(URL)
#to get the lyrics out of the page I used beautifulsoup in the following way.
page = requests.get(url)
html = BeautifulSoup(page.text, 'html.parser')
lyrics = html.find('div', class_='lyrics').get_text()
lyrics = re.sub(r'[\(\[].*?[\)\]]', '', lyrics)
print(lyrics)
希望这会有所帮助。