Where do I find the Google Places API Client Libra

2019-05-07 06:13发布

问题:

It's not under the supported libraries here: https://developers.google.com/api-client-library/python/reference/supported_apis

Is it just not available with Python? If not, what language is it available for?

回答1:

Andre's answer points you at a correct place to reference the API. Since your question was python specific, allow me to show you a basic approach to building your submitted search URL in python. This example will get you all the way to search content in just a few minutes after you sign up for Google's free API key.

ACCESS_TOKEN = <Get one of these following the directions on the places page>

import urllib

def build_URL(search_text='',types_text=''):
    base_url = 'https://maps.googleapis.com/maps/api/place/textsearch/json'     # Can change json to xml to change output type
    key_string = '?key='+ACCESS_TOKEN                                           # First think after the base_url starts with ? instead of &
    query_string = '&query='+urllib.quote(search_text)
    sensor_string = '&sensor=false'                                             # Presumably you are not getting location from device GPS
    type_string = ''
    if types_text!='':
        type_string = '&types='+urllib.quote(types_text)                        # More on types: https://developers.google.com/places/documentation/supported_types
    url = base_url+key_string+query_string+sensor_string+type_string
    return url

print(build_URL(search_text='Your search string here'))

This code will build and print a URL searching for whatever you put in the last line replacing "Your search string here". You need to build one of those URLs for each search. In this case I've printed it so you can copy and paste it into your browser address bar, which will give you a return (in the browser) of a JSON text object the same as you will get when your program submits that URL. I recommend using the python requests library to get that within your program and you can do that simply by taking the returned URL and doing this:

response = requests.get(url)

Next up you need to parse the returned response JSON, which you can do by converting it with the json library (look for json.loads for example). After running that response through json.loads you will have a nice python dictionary with all your results. You can also paste that return (e.g. from the browser or a saved file) into an online JSON viewer to understand the structure while you write code to access the dictionary that comes out of json.loads.

Please feel free to post more questions if part of this isn't clear.



回答2:

Somebody has written a wrapper for the API: https://github.com/slimkrazy/python-google-places

Basically it's just HTTP with JSON responses. It's easier to access through JavaScript but it's just as easy to use urllib and the json library to connect to the API.



回答3:

Ezekiel's answer worked great for me and all of the credit goes to him. I had to change his code in order for it to work with python3. Below is the code I used:

def build_URL(search_text='',types_text=''):
    base_url = 'https://maps.googleapis.com/maps/api/place/textsearch/json'
    key_string = '?key=' + ACCESS_TOKEN
    query_string = '&query=' + urllib.parse.quote(search_text)
    type_string = ''
    if types_text != '':
        type_string = '&types='+urllib.parse.quote(types_text)
    url = base_url+key_string+query_string+type_string
    return url

The changes were urllib.quote was changed to urllib.parse.quote and sensor was removed because google is deprecating it.