Использую
Google Places API на сервере.
Причем такая строка работает:
>>> url
'https://maps.googleapis.com/maps/api/place/queryautocomplete/json?key=AIzaSyDDRQjJN5IIrMzl4i9Po670KmuKP5ENAhM&language=ru&input=pizza+near%20par'
>>> response = urllib2.urlopen(url)
>>> data = json.loads(response.read())
>>> data
{u'status': u'OK', u'predictions': [{u'structured_formatting': {u'secondary_text_matched_substrings': [{u'length': 3, u'offset': 8}], u'secondary_text': u'\u0440\u044f\u0434\u043e\u043c \u0441 Paris, France', u'main_text_matched_substrings': [{u'length': 5, u'offset': 0}], u'main_text': u'pizza'}, u'matched_substrings': [{u'length': 5, u'offset': 0}, {u'length': 3, u'offset': 14}], u'terms': [{u'value': u'pizza', u'offset': 0}, {u'value': u'\u0440\u044f\u0434\u043e\u043c \u0441', u'offset': 6}, {u'value': u'Paris', u'offset': 14}, {u'value': u'France', u'offset': 21}], u'description': u'pizza \u0440\u044f\u0434\u043e\u043c \u0441 Paris, France'}, {u'structured_formatting': {u'secondary_text_matched_substrings': [{u'length': 3, u'offset': 8}], u'secondary_text': u'\u0440\u044f\u0434\u043e\u043c \u0441 Paramus, NJ, United States', u'main_text_matched_substrings': [{u'length': 5, u'offset': 0}], u'main_text': u'pizza'}, u'matched_substrings': [{u'length': 5, u'offset': 0}, {u'length': 3, u'offset': 14}], u'terms': [{u'value': u'pizza', u'offset': 0}, {u'value': u'\u0440\u044f\u0434\u043e\u043c \u0441', u'offset': 6}, {u'value': u'Paramus', u'offset': 14}, {u'value': u'NJ', u'offset': 23}, {u'value': u'United States', u'offset': 27}], u'description': u'pizza \u0440\u044f\u0434\u043e\u043c \u0441 Paramus, NJ, United States'}, {u'structured_formatting': {u'secondary_text_matched_substrings': [{u'length': 3, u'offset': 8}], u'secondary_text': u'\u0440\u044f\u0434\u043e\u043c \u0441 Paris, Salles, France', u'main_text_matched_substrings': [{u'length': 5, u'offset': 0}], u'main_text': u'pizza'}, u'matched_substrings': [{u'length': 5, u'offset': 0}, {u'length': 3, u'offset': 14}], u'terms': [{u'value': u'pizza', u'offset': 0}, {u'value': u'\u0440\u044f\u0434\u043e\u043c \u0441', u'offset': 6}, {u'value': u'Paris', u'offset': 14}, {u'value': u'Salles', u'offset': 21}, {u'value': u'France', u'offset': 29}], u'description': u'pizza \u0440\u044f\u0434\u043e\u043c \u0441 Paris, Salles, France'}, {u'structured_formatting': {u'secondary_text_matched_substrings': [{u'length': 3, u'offset': 8}], u'secondary_text': u'\u0440\u044f\u0434\u043e\u043c \u0441 Parramatta, New South Wales, Australia', u'main_text_matched_substrings': [{u'length': 5, u'offset': 0}], u'main_text': u'pizza'}, u'matched_substrings': [{u'length': 5, u'offset': 0}, {u'length': 3, u'offset': 14}], u'terms': [{u'value': u'pizza', u'offset': 0}, {u'value': u'\u0440\u044f\u0434\u043e\u043c \u0441', u'offset': 6}, {u'value': u'Parramatta', u'offset': 14}, {u'value': u'New South Wales', u'offset': 26}, {u'value': u'Australia', u'offset': 43}], u'description': u'pizza \u0440\u044f\u0434\u043e\u043c \u0441 Parramatta, New South Wales, Australia'}, {u'structured_formatting': {u'secondary_text_matched_substrings': [{u'length': 3, u'offset': 8}], u'secondary_text': u'\u0440\u044f\u0434\u043e\u043c \u0441 Park Avenue, NY, United States', u'main_text_matched_substrings': [{u'length': 5, u'offset': 0}], u'main_text': u'pizza'}, u'matched_substrings': [{u'length': 5, u'offset': 0}, {u'length': 3, u'offset': 14}], u'terms': [{u'value': u'pizza', u'offset': 0}, {u'value': u'\u0440\u044f\u0434\u043e\u043c \u0441', u'offset': 6}, {u'value': u'Park Avenue', u'offset': 14}, {u'value': u'NY', u'offset': 27}, {u'value': u'United States', u'offset': 31}], u'description': u'pizza \u0440\u044f\u0434\u043e\u043c \u0441 Park Avenue, NY, United States'}]}
Но если в дело вступает кириллица, то все ломается:
>>> url = 'https://maps.googleapis.com/maps/api/place/queryautocomplete/json?key='+ key + '&language=ru&input=Феодосия'
>>> response = urllib2.urlopen(url)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "C:\Python27\lib\urllib2.py", line 435, in open
response = meth(req, response)
File "C:\Python27\lib\urllib2.py", line 548, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python27\lib\urllib2.py", line 473, in error
return self._call_chain(*args)
File "C:\Python27\lib\urllib2.py", line 407, in _call_chain
result = func(*args)
File "C:\Python27\lib\urllib2.py", line 556, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 400: Bad Request
Даже, если делаю urllib2.quote (аналог encodeURI в JS), то ничего не меняется:
>>> url = urllib2.quote('https://maps.googleapis.com/maps/api/place/queryautocomplete/json?key=' + key + '&language=ru&input=Феодосия', safe='~@#$&()*!+=:;,.?/\'')
>>> url
'https://maps.googleapis.com/maps/api/place/queryautocomplete/json?key=AIzaSyDDRQjJN5IIrMzl4i9Po670KmuKP5ENAhM&language=ru&input=%94%A5%AE%A4%AE%E1%A8%EF'
>>> response = urllib2.urlopen(url)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "C:\Python27\lib\urllib2.py", line 435, in open
response = meth(req, response)
File "C:\Python27\lib\urllib2.py", line 548, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python27\lib\urllib2.py", line 473, in error
return self._call_chain(*args)
File "C:\Python27\lib\urllib2.py", line 407, in _call_chain
result = func(*args)
File "C:\Python27\lib\urllib2.py", line 556, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 400: Bad Request
Не понимаю что не так?