Created
July 16, 2020 14:00
-
-
Save vaclavdekanovsky/0e4156831e0f5f8c8c7c9a7301062250 to your computer and use it in GitHub Desktop.
Google API via Python's requests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import requests\n", | |
| "import json\n", | |
| "import urllib" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 9, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "# https://developers.google.com/maps/documentation/geocoding/intro\n", | |
| "base_url= \"https://maps.googleapis.com/maps/api/geocode/json?\"\n", | |
| "AUTH_KEY = \"Your Key\"" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 10, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "# set up your search parameters - address and API key\n", | |
| "parameters = {\"address\": \"Tuscany, Italy\",\n", | |
| " \"key\": AUTH_KEY}" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 11, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "https://maps.googleapis.com/maps/api/geocode/json?address=Tuscany%2C+Italy&key=Your+Key\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# urllib.parse.urlencode turns parameters into url\n", | |
| "print(f\"{base_url}{urllib.parse.urlencode(parameters)}\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "r = requests.get(f\"{base_url}{urllib.parse.urlencode(parameters)}\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'results': [{'address_components': [{'long_name': 'Tuscany',\n", | |
| " 'short_name': 'Tuscany',\n", | |
| " 'types': ['administrative_area_level_1', 'political']},\n", | |
| " {'long_name': 'Italy',\n", | |
| " 'short_name': 'IT',\n", | |
| " 'types': ['country', 'political']}],\n", | |
| " 'formatted_address': 'Tuscany, Italy',\n", | |
| " 'geometry': {'bounds': {'northeast': {'lat': 44.4726899, 'lng': 12.3713555},\n", | |
| " 'southwest': {'lat': 42.2376686, 'lng': 9.6867213}},\n", | |
| " 'location': {'lat': 43.7710513, 'lng': 11.2486208},\n", | |
| " 'location_type': 'APPROXIMATE',\n", | |
| " 'viewport': {'northeast': {'lat': 44.4726899, 'lng': 12.3713555},\n", | |
| " 'southwest': {'lat': 42.2376686, 'lng': 9.6867213}}},\n", | |
| " 'place_id': 'ChIJezSAEFMr1BIRq1kgW7rDxro',\n", | |
| " 'types': ['administrative_area_level_1', 'political']}],\n", | |
| " 'status': 'OK'}" | |
| ] | |
| }, | |
| "execution_count": 6, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "data = json.loads(r.content)\n", | |
| "data" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 7, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'lat': 43.7710513, 'lng': 11.2486208}" | |
| ] | |
| }, | |
| "execution_count": 7, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "data.get(\"results\")[0].get(\"geometry\").get(\"location\")" | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 3", | |
| "language": "python", | |
| "name": "python3" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 3 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython3", | |
| "version": "3.7.4" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 2 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment