K
K
koi com2016-12-31 12:06:29
Python
koi com, 2016-12-31 12:06:29

How to make a request to the Google Places API using Python?

I am using the Google Places API on the server.
And this line works:

>>> 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'}]}

But if the Cyrillic alphabet comes into play, then everything breaks down:
>>> 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

Even if I do urllib2.quote (analogous to encodeURI in JS), nothing changes:
>>> url = urllib2.quote('https://maps.googleapis.com/maps/api/place/queryautocomplete/json?key=' + key + '&language=ru&input=Феодосия', safe='[email protected]#$&()*!+=:;,.?/\'')
>>> 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

I don't understand what's wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Great274, 2017-01-06
@Great274

Cyrillic must be stored in Unicode

>>> key = u'AIzaSyDDRQjJN5IIrMzl4i9Po670KmuKP5ENAhM'
>>> url = u'https://maps.googleapis.com/maps/api/place/queryautocomplete/json?key=%s&language=ru&input=Феодосия' % key

>>> response = urllib2.urlopen(url)

>>> data = json.loads(response.read())

>>> data

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question