Ekstrak data Google Maps dari tempat terdekat dan simpan di Google Spreadsheet (Google APIS dengan Python)
Apr 08 2023
Di sini, niat saya adalah untuk berbagi hal-hal praktis dan untuk teori Anda dapat mencarinya di Google dan membaca sebanyak yang Anda bisa. Dapatkan Kredensial dari konsol pengembang Google: Di https://console.
Di sini, niat saya adalah untuk berbagi hal-hal praktis dan untuk teori Anda dapat mencarinya di Google dan membaca sebanyak yang Anda bisa.
Dapatkan Kredensial dari konsol pengembang Google:
Padahttps://console.developers.google.com/apisarahkan ke Kredensial pada tab dan klik BUAT KREDENSIAL untuk menerima Kunci API.
Kredensial yang Diperlukan :
1. Kunci API (untuk mengakses spreadsheet google)
2. token.json (untuk masuk dan menyegarkan token)
3. kredensial.json (konfigurasi khusus proyek konsol google)
Skrip kode python:
import os
import time
import requests
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
# The ID and range of a sample spreadsheet.
SPREADSHEET_ID = '<YOUR-GOOGLE-SPREADSHEET-ID>'
RANGE_NAME = 'Sheet1!A1:B'
def get_places(api_url, params, values):
time.sleep(3)
# Make a request to the Places API
response = requests.get(api_url, params=params).json()
results = response.get("results", [])
# Loop through the results and add them to the values list
for result in results:
name = result["name"]
values.append([name])
# Check if there are more results
next_page_token = response.get("next_page_token")
if next_page_token:
# Call the API again with the pagetoken parameter
params["pagetoken"] = next_page_token
get_places(api_url, params, values)
def places():
# Make a request to the Places API
# Get the first page of results from the Google Maps Places API
api_url = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json'
params = {
'location': '31.7782902,76.3448069',
'rankby': 'distance',
'key': '<YOUR-API-KEY>'
}
values = []
get_places(api_url, params, values)
# Write the values to the spreadsheet
body = {
'values': values
}
return body
def main():
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.json', 'w') as token:
token.write(creds.to_json())
try:
service = build('sheets', 'v4', credentials=creds)
# Write values to the spreadsheet
body=places()
result = service.spreadsheets().values().update(
spreadsheetId=SPREADSHEET_ID, range=RANGE_NAME,
valueInputOption='USER_ENTERED', body=body).execute()
print(f'{result.get('updatedCells')} cells updated.')
except HttpError as err:
print(err)
if __name__ == '__main__':
main()
- Kesimpulan
- Referensi
Jana Duggar: Semua yang Dia Katakan Tentang Cinta dan Jendela 5 Tahunnya untuk Menemukan 'Yang Satu'

![Apa itu Linked List? [Bagian 1]](https://post.nghiatu.com/assets/images/m/max/724/1*Xokk6XOjWyIGCBujkJsCzQ.jpeg)



































