-1

Ich habe diesen Code geschrieben. Es funktioniert perfekt Google-Maps-api Informationen an eine strukturierte CSV-Datei zu erhalten:Erstellen Sie eine Tkinter-Schnittstelle mit einer Schaltfläche, die mit einer Google Places-Abfragefunktion verknüpft ist.

def getPlaces(location,radius,i,j,k): 
    url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location="+location+"&radius="+radius 
    r = requests.get(url+"&types="+"establishment"+"&key="+API) 
    response = r.json() 
    results = [] 
    for result in response['results']: 
     results.append(result) 
    if len(results)<20: 
     print('next type') 
    return results 

Es sollte zurückgeben eine json Struktur:

[{'geometry': {'location': {'lat': -1.526394, 'lng': -78.001286},
'viewport': {'northeast': {'lat': -1.525045019708498, 'lng': -77.99993701970848}, 'southwest': {'lat': -1.527742980291502, 'lng': -78.0026349802915}}}, 'icon': ' https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png ', 'id': '69e54af1e371137add1acca484b0fc7845184b1f', 'name': 'zoorefugio tarqui', 'opening_hours': {'open_now': True, 'weekday_text': []}, 'photos': [{'height': 2592, 'html_attributions': [' Franklin Guaman '], 'photo_reference': 'CmRaAAAAWBe-CXcL8f52LopjP_nvS1BvtIrdhK5-XvYwG2oA9tS2oh3dzuyYlVIg_3z72mhf2NZ7Sv2S38UDm8J5lZkiDtC8hTZsLT9phKOA54i_W-DcuZQVnESGvHAizXRV4t1xEhAEx9KPnD5vjORlqLdVtrJEGhQ6YnBLp1P0GITIr8fEUB6a5WducA', 'width': 4608}], 'place_id': 'ChIJRePuE4re05ERVpc1cgpIogQ', 'rating': 4.5, 'reference': 'CmRRAAAA0MBgQLzubtDJGAJGZDW7wuZFujGVq6bNQNAvsMj5mQOVV9mNfmxKxlACFuC1UL8exB2AEtj63yiraasnbAHFf7TmBSd7bLZ1b_HFd2H6Z5JbJ5sIAS1CA3dQ-lWsMAIAEhBVgsIvcMM2aPdCUU6739lzGhT7o9nZun5tM0klSzyQf8nNhkGDzg', 'scope': 'GOOGLE', 'types': ['zoo', 'bar', 'restaurant',
'food', 'point_of_interest', 'establishment'], 'vicinity': 'Parroquia Tarqui, 160150 Puyo, Ecuador, Puyo'}]

Der nächste Code, json in einen Rahmen verwandeln und speichern sie sie als CSV in eine Datei:

def placesCsv(location,radius,i,j,k): 
    places_jn=json_normalize(getPlaces(location,radius,i,j,k)) 
    s = places_jn.apply(lambda x: pd.Series(x['types']),axis=1).stack().reset_index(level=1, drop=True) 
    s.name = 'types' 
    places_df.drop_duplicates(subset=['id','types'], keep='first', inplace=True) 
    places_df.to_csv('google_places'+str(location)+'.csv') 

ich erhalten diese Ergebnisse, und sie werden als csv in der Set-Datei gespeichert:

> next type 
> next type amusement_park API: 20 
> 2 20 2 
> next type aquarium API: 20 
> 2 20 3 
> Did not find results for the type aquarium 
> next type art_gallery API: 20 
> 2 20 4 
> Did not find results for the type art_gallery 
> this was the final type 

Wie Sie sehen, gibt es nicht die ersten beiden Arten Flughafen und Buchhaltung, weil es keine von ihnen gefunden hat. Dann hat es keine art_gallery gefunden, deshalb gibt es "Ergebnisse für den Typ art_gallery nicht gefunden" zurück.

Nun, ich versuche, eine Tkinter-Schnittstelle zu schaffen, um die API zu machen konsultieren wie:

from tkinter import * 
from functools import partial 
def get_entry_fields(): 
    lat=e1.get() 
    lng=e2.get() 
    latlng=str(lat)+','+str(lng) 

def radius(): 
    radius=str(e3.get()) 

def look(): 
    print("Single Click, Button-l") 
    places=placesCsv(location=str(get_entry_fields()), 
        radius=str(radius()), 
        i=0, 
        j=10, 
        k=0) 

def quit():       
    print("Right Click, so let's stop") 
    widget.destroy 

master = Tk() 
Label(master, text="lat").grid(row=0) 
Label(master, text="lng").grid(row=1) 
Label(master, text="radius").grid(row=2) 

e1=Entry(master) 
e2=Entry(master) 
e3=Entry(master) 

e1.grid(row=0, column=1) 
e2.grid(row=1, column=1) 
e3.grid(row=2, column=1) 

Button(master, text='Quit', command=master.destroy).grid(row=4, column=2, sticky=W, pady=4) 
Button(master, text='Show', command=look).grid(row=4, column=0, sticky=W, pady=4) 
mainloop() 

Wenn ich die Funktion über die Schnittstelle mit dem gleichen lat, lng und Radius laufen und die Ausgabe wie folgt aussieht:

> Single Click, Button-l 
> next type 
> next type amusement_park10 
> 0 10 2 
> next type aquarium10 
> 0 10 3 
> next type art_gallery10 
> 0 10 4 
> this was the final type 

Wie Sie sehen, es gibt nicht die ersten beiden Typen Ariport und Buchhaltung, weil es nicht von ihnen gefunden werden. Es hat andere Orte gefunden, aber die Liste "Ergebnisse" hat eine Länge Null. Dann hat die JSON-Datei keine Liste von Typen. So bekomme ich diesen Fehler:

> Exception in Tkinter callback 
> Traceback (most recent call last): File 
> "C:\Users\NA401134\AppData\Local\Continuum\anaconda3\lib\tkinter\__init__.py", 
> line 1699, in __call__ 
>  return self.func(*args) File "<ipython-input-50-8e3a3f48613d>", line 32, in look 
>  k=0) File "<ipython-input-42-4681f83b6314>", line 5, in placesCsv 
>  places_df=places_jn.drop('types', axis=1).join(s) File "C:\Users\NA401134\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\generic.py", 
> line 2161, in drop 
>  new_axis = axis.drop(labels, errors=errors) File >"C:\Users\NA401134\AppData\Local\Continuum\anaconda3\lib\site->packages\pandas\core\indexes\base.py", 
> line 3624, in drop 
>  labels[mask]) ValueError: labels ['types'] not contained in axis 

Wie Sie sehe ich nicht json verwandeln zu gestalten, weil es keine Anzeigen in Typen, wenn die konsultieren über die Schnittstelle erfolgt.

+0

Verwendung Taste '{}' Fehlermeldungen zu formatieren - werden sie besser formatiert werden. – furas

+0

BTW: 'widget.destroy()' in 'quit()' benötigt '()' am Ende. – furas

+0

Sie könnten alle Funktionen am Anfang - vor 'master = Tk()' - setzen, um Code lesbarer zu machen. – furas

Antwort

0

Der Code funktioniert wie:

def get_entry_fields(lat,lng): 
    latlng=str(lat)+','+str(lng) 
    return(latlng) 


def look(): 
    print("Single Click, Button-l") 
    places=PlacesCsv(
      location=get_entry_fields(
        lat=e1.get(), 
        lng=e2.get() 
        ), 
        radius=str(
          e3.get() 
          ), 
        i=0, 
        j=100, 
        k=0) 

def quit():       
    print("Right Click, so let's stop") 
    widget.destroy() 

master = Tk() 
Label(master, text="lat").grid(row=0) 
Label(master, text="lng").grid(row=1) 
Label(master, text="radius").grid(row=2) 

e1=Entry(master) 
e2=Entry(master) 
e3=Entry(master) 

e1.grid(row=0, column=1) 
e2.grid(row=1, column=1) 
e3.grid(row=2, column=1) 

Button(master, text='Quit', command=master.destroy).grid(row=4, column=2, sticky=W, pady=4) 
Button(master, text='Show', command=look).grid(row=4, column=0, sticky=W, pady=4) 
mainloop() 
Verwandte Themen