Seite 2 von 2

Re: Python Google Places API

Verfasst: Dienstag 22. Oktober 2019, 18:37
von illy1
__blackjack__ hat geschrieben: Dienstag 22. Oktober 2019, 18:06 @illy1: Aber was sind denn dann ”Fields”? Du behandelst es ja dann als wäre es wieder das komplette Wörterbuch, und das in einer Schleife, das macht alles gar keinen Sinn.
Aber wieso nicht?

Ich möchte ja nur, dass er so wie in Zeile 18 das selbe Prinzip mit einer anderen URL wiederholt.

Re: Python Google Places API

Verfasst: Dienstag 22. Oktober 2019, 19:06
von illy1
Nun habe das Problem gelöst.. Hat wirklich keinen Sinn gemacht sort eine Schleife zu platzieren..

Nun kommt aber das nächste Problem :D

Code: Alles auswählen

search = input("Bitte Stichwort für Ortssuche eingeben: ")
query= "query="+search
find_places = "https://maps.googleapis.com/maps/api/place/textsearch/json?"+query+"&key="+APIKey
url_json = urllib.request.urlopen(find_places)
jsonf = json.load(url_json)
for place in jsonf['results']:
    place_id = place['place_id']
    details = "https://maps.googleapis.com/maps/api/place/details/json?place_id="+place_id+"&fields=formatted_phone_number&key="+APIKey
    url_json2 = urllib.request.urlopen(details)
    jsondetails = json.load(url_json2)
    dir_details = jsondetails['result']
    print(place['name']+" "+place['formatted_address']+" "+dir_details['formatted_phone_number']+" "+place['rating'])

Code: Alles auswählen

Traceback (most recent call last):
  File "d:/isaac/Programmierung/Python_uebungen/GoogleAPI.py", line 24, in <module>
    print(place['name']+" "+place['formatted_address']+" "+dir_details['formatted_phone_number']+" "+place['rating'])
TypeError: can only concatenate str (not "int") to str
Die ratings werden jeweils mit "." angegeben... liegt das daran?

VG

Re: Python Google Places API

Verfasst: Dienstag 22. Oktober 2019, 19:14
von __deets__
Hast du die Fehlermeldung mal gelesen? Oder gar gegoogelt?

Re: Python Google Places API

Verfasst: Dienstag 22. Oktober 2019, 20:15
von illy1
Habs gefunden dann... Befehl hieß str..

Danke für die Hilfe, Thread kann geschlossen werden.

Re: Python Google Places API

Verfasst: Mittwoch 23. Oktober 2019, 05:32
von snafu
Man könnte auch einfach print() richtig benutzen und Kommas verwenden. Dann wird jedes Argument automatisch in eine Zeichenkette verwandelt und die Leerzeichen gibt es gratis dazu. 😌

Re: Python Google Places API

Verfasst: Mittwoch 23. Oktober 2019, 07:45
von Sirius3
URL-Parameter sollte man nicht einfach per + zusammenstückeln, weil Sonderzeichen spezielle kodiert werden müssen.
URLs die man öffnet sollte man auch wieder schließen, am besten zusammen mit dem with-Statement.

Code: Alles auswählen

import urllib.request
import urllib.parse

API_KEY = "xyz"
PLACES_URL = "https://maps.googleapis.com/maps/api/place/{}/json?{}"

search = input("Bitte Stichwort für Ortssuche eingeben: ")
params = urllib.parse.urlencode({'query': search, 'key': API_KEY})
with urllib.request.urlopen(PLACES_URL.format("textsearch", params)) as response
    places = json.load(response)['results']
for place in places:
    params = urllib.parse.urlencode({'place_id': place['place_id'],
        'fields': 'formatted_phone_number', 'key': API_KEY})
    with urllib.request.urlopen(PLACES_URL.format("details", params)) as response
        details = json.load(response)['results']
    print(place['name'], place['formatted_address'], details['formatted_phone_number'], place['rating'])