Закодируйте URL-адрес Rest API в python, чтобы избежать символов, отличных от ascii, таких как '\ xa0' - PullRequest
0 голосов
/ 17 июня 2020

Я использовал следующий код для получения данных из API с помощью python, запустив его в Google Colab.

def HereAPI(auth_key,lat,lon):
    location = str(lat) + ',' + str(lon)
    MyUrl = ('https://places.ls.hereapi.com/places/v1/browse'
            '?apiKey=%s'
            '&in=%s'
            ';r=10000'
            '&cat=restaurant&pretty') % (auth_key,location)
    MyUrl.replace(u'\xa0', '')
    #grabbing the JSON result
    jsonRaw = urllib.request.urlopen(MyUrl).read()
    #str(jsonRaw).encode('ascii', 'ignore')
    jsonData = json.loads(jsonRaw)
    return jsonData

# Function call
df['output'] = df.apply(lambda x: HereAPI(auth_key,x['lat'],x['long']), axis=1)

Я получаю следующую ошибку:

# Non-ASCII characters should have been eliminated earlier
-> 1134         self._output(request.encode('ascii'))
   1135 
   1136         if self._http_vsn == 11:

UnicodeEncodeError: 'ascii' codec can't encode character '\xa0' in position 86: ordinal not in range(128)

Может ли кто-нибудь предложить изменения в ошибке - чтобы пропустить ошибку

...