[Solved] Calling the Baidu location retrieval API reports an error UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 27-32:ordinal

Calling Baidu location retrieval API to report an error UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 27-32:ordinal not in range(128)

Writing when calling the API:
https://api.map.baidu.com/place/v2/search?query=hospital&region=Chengdu&output=json&page_size=10&page_num=0&ak = own AK

The first error is that after copying the link to the address bar and opening it, status200 appears, indicating that the APP does not exist, and there is a problem with the AK. Solution: Do not add quotation marks to the string after the parameter and AK

After confirming that the path is correct, use the following code to read json

path=urllib.request.urlopen(d)
m=json.loads(path.read())

The result is an error UnicodeEncodeError: ascii’ codec can’t encode characters in position 27-32:ordinal not in range(128). The reason is that the default encoding of Python3 is utf-8, and Simplified Chinese appears in the path. This part of the encoding needs to be changed to gbk.

Solution: Change the Simplified Chinese part to str((‘Chengdu’).encode(‘gbk’))