代码之家  ›  专栏  ›  技术社区  ›  tbd_

在Spotify的搜索API中更正python语法以按艺术家搜索?

  •  2
  • tbd_  · 技术社区  · 7 年前

    艺术家使用Spotify的API进行搜索时,正确的python语法是什么?也许我错过了一些明显的东西(盯着这个看了太久)


    https://developer.spotify.com/web-api/search-item/

    artist_name = 'Linkin%20Park'
    artist_info = requests.get('https://api.spotify.com/v1/search', header = {'access_token': access_token}, q = artist_name, type = 'artist')
    
    ERROR: TypeError: requests() got an unexpected keyword argument 'q'
    

    artist_info = requests.get('https://api.spotify.com/v1/search', header = {'access_token': access_token}, query = list(q = artist_name, type = 'artist'))
    

    ERROR: TypeError: list() takes at most 1 argument (2 given)
    
    3 回复  |  直到 7 年前
        1
  •  4
  •   Ilja Everilä    7 年前

    list() builtin 接受0或1位置参数,该参数应为iterable。我强烈建议你通过官方 tutorial

    你可能正在使用 python-requests 图书馆为了传递查询参数,例如 q 参数 you'd pass a dict of parameters as the params argument

    artist_info = requests.get(
        'https://api.spotify.com/v1/search',
        headers={ 'access_token': access_token },
        params={ 'q': artist_name, 'type': 'artist' })
    

    must be in its plural form, not "header" .

    最后,你可能对 spotipy

        2
  •  3
  •   petezurich rdelmar    7 年前

    @Ilja和@alfasin的回答提供了很好的指导,但似乎不再有效。

    你必须换到 headers authorization Bearer .

    artist_info = requests.get('https://api.spotify.com/v1/search',
        headers={ 'authorization': "Bearer " + token}, 
        params={ 'q': artist_name, 'type': 'artist' })
    
        3
  •  2
  •   Nir Alfasi    7 年前

    artist_info = requests.get('https://api.spotify.com/v1/search?q={}&type={}'.format(artist_name, 'artist'), header = {'access_token': access_token})