Python、Google 地点 API - "Invalid request. One of the input parameters contains a non-UTF-8 string"

Python, Google Places API - "Invalid request. One of the input parameters contains a non-UTF-8 string"

我正在尝试使用 Google 个地方 API,但在我的一些请求中收到以下错误消息:

Invalid request. One of the input parameters contains a non-UTF-8 string.

例如,以下请求 - https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=WRAFTON%HOUSE%SURGERY%WRAFTON%HOUSE%SURGERY%9%11%WELLFIELD%ROAD%HATFIELD%HERTFORDSHIRE%AL10%0BS&inputtype=textquery&fields=place_id,photos,formatted_address,name,rating,geometry,types&key=MY_API_KEY - 正在返回:

{
   "candidates" : [],
   "error_message" : "Invalid request. One of the input parameters contains a non-UTF-8 string.",
   "status" : "INVALID_REQUEST"
}

但是如果我删除数字,如 - https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=WRAFTON%HOUSE%SURGERY%WRAFTON%HOUSE%SURGERY%WELLFIELD%ROAD%HATFIELD%HERTFORDSHIRE%&inputtype=textquery&fields=place_id,photos,formatted_address,name,rating,geometry,types&key=MY_API_KEY - 我会得到预期的数据:

{
   "candidates" : [
      {
         "formatted_address" : "9-11 Wellfield Rd, Hatfield AL10 0BS, United Kingdom",
         "geometry" : {
            "location" : {
               "lat" : 51.7659979,
               "lng" : -0.2269138
            },
            "viewport" : {
               "northeast" : {
                  "lat" : 51.76722407989272,
                  "lng" : -0.2257391701072778
               },
               "southwest" : {
                  "lat" : 51.76452442010727,
                  "lng" : -0.2284388298927222
               }
            }
         },
         "name" : "Wrafton House Surgery NHS",
         "photos" : [
            {
               "height" : 2448,
               "html_attributions" : [
                  "\u003ca href=\"https://maps.google.com/maps/contrib/101621497138346721555/photos\"\u003eGicu Razoare\u003c/a\u003e"
               ],
               "photo_reference" : "CmRaAAAABVxpTtU1jnkajKeMys5GsNFha4PMisyTPaw4nyhpQMH0hwOu2kDDnGhKWXpsfiXqmBAGyO5ctA86dbtavh0xWZEspctQXo1EAcJswgxdnl0zSzG5b2xMbzSK6P6lRv2QEhCIPKzSjVUEQBSM3xutYsrwGhTGcax2XoglbtUgKsRFSIJF2oaQwA",
               "width" : 3264
            }
         ],
         "place_id" : "ChIJ85ML6oI8dkgRKziTLkoRGeA",
         "rating" : 1.9,
         "types" : [ "hospital", "point_of_interest", "establishment" ]
      }
   ],
   "status" : "OK"
}

这是因为数字吗?有没有办法通过不删除它们来解决这个问题?

导致此错误的原因是“9%11”和"AL10%0BS"中的“%”字符。您需要 encode 正确 URL。

试试这个:https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=Wrafton%20House%20Surgery%2C%209-11%20Wellfield%20Road%2C%20Hatfield%2C%20Hertfordshire%20AL10%200BS&inputtype=textquery&fields=place_id,photos,formatted_address,name,rating,geometry,types&key=YOUR_API_KEY

希望对您有所帮助!