将Google Colab与Google Maps API结合使用,但可以';t获取包含要提取的地址和电话号码详细信息的文件
第一次海报和初学者编码器在这里 我想提取一份给定地理位置的餐馆列表及其详细信息,包括电话号码。下面是我所能得到的,但我甚至不能从GoogleColab中提取CSV。关于如何让文件显示在我的文件中以便提取它,有什么建议吗将Google Colab与Google Maps API结合使用,但可以';t获取包含要提取的地址和电话号码详细信息的文件,api,csv,google-maps,extract,Api,Csv,Google Maps,Extract,第一次海报和初学者编码器在这里 我想提取一份给定地理位置的餐馆列表及其详细信息,包括电话号码。下面是我所能得到的,但我甚至不能从GoogleColab中提取CSV。关于如何让文件显示在我的文件中以便提取它,有什么建议吗 import pandas as pd, numpy as np import requests import json import time from google.colab import files final_data = [] class GooglePlaces(
import pandas as pd, numpy as np
import requests
import json
import time
from google.colab import files
final_data = []
class GooglePlaces(object):
def __init__(self, apiKey):
super(GooglePlaces, self).__init__()
self.apiKey = apiKey
def search_places_by_coordinate(self, location, radius, types):
endpoint_url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json"
places = []
params = {
'location': location,
'radius': radius,
'types': types,
'key': self.apiKey
}
res = requests.get(endpoint_url, params = params)
results = json.loads(res.content)
places.extend(results['results'])
time.sleep(2)
while "next_page_token" in results:
params['pagetoken'] = results['next_page_token'],
res = requests.get(endpoint_url, params = params)
results = json.loads(res.content)
places.extend(results['results'])
time.sleep(2)
return places
def get_place_details(self, place_id, fields):
endpoint_url = "https://maps.googleapis.com/maps/api/place/details/json"
params = {
'placeid': place_id,
'fields': ",".join(fields),
'key': self.apiKey
}
res = requests.get(endpoint_url, params = params)
place_details = json.loads(res.content)
return place_details
if __name__ == '__main__':
api = GooglePlaces("INSERT API KEY HERE")
places = api.search_places_by_coordinate("43.7887075,-79.4464409", "100", "restaurant")
fields = ['name', 'formatted_address', 'international_phone_number', 'website', 'rating', 'review']
for place in places:
details = api.get_place_details(place['place_id'], fields)
try:
website = details['result']['website']
except KeyError:
website = ""
try:
name = details['result']['name']
except KeyError:
name = ""
try:
address = details['result']['formatted_address']
except KeyError:
address = ""
try:
phone_number = details['result']['international_phone_number']
except KeyError:
phone_number = ""
try:
reviews = details['result']['reviews']
except KeyError:
reviews = []
print("===================PLACE===================")
print("Name:", name)
print("Website:", website)
print("Address:", address)
print("Phone Number", phone_number)
print("==================REVIEWS==================")
for review in reviews:
author_name = review['author_name']
rating = review['rating']
text = review['text']
time = review['relative_time_description']
profile_photo = review['profile_photo_url']
print("Author Name:", author_name)
print("Rating:", rating)
print("Text:", text)
print("Time:", time)
print("Profile photo:", profile_photo)
print("-----------------------------------------")
data = [name, website, address, phone_number]
final_data.append(data)
export_dataframe_1_medium = pd.DataFrame(final_data, columns=['Name','Website', 'Address', 'Phone Number'])
export_dataframe_1_medium.to_csv('export_dataframe_1_medium.csv')
请提供尝试运行代码时返回的错误