使用Python和Google places API收集位置



我想用latlong(0.507068,101.447777(收集我的城市Pekanbaru周围的地方,我会将其转换为数据集。数据集(它包含place_name、place_id、lat、long和type列(。

下面是我尝试过的脚本。

import json
import urllib.request as url_req
import time
import pandas as pd
NATAL_CENTER = (0.507068,101.447777)
API_KEY = 'API'
API_NEARBY_SEARCH_URL = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json'
RADIUS = 30000
PLACES_TYPES = [('airport', 1), ('bank', 2)] ## TESTING
# PLACES_TYPES = [('airport', 1), ('bank', 2), ('bar', 3), ('beauty_salon', 3), ('book_store', 1), ('cafe', 1), ('church', 3), ('doctor', 3), ('dentist', 2), ('gym', 3), ('hair_care', 3), ('hospital', 2), ('pharmacy', 3), ('pet_store', 1), ('night_club', 2), ('movie_theater', 1), ('school', 3), ('shopping_mall', 1), ('supermarket', 3), ('store', 3)]
def request_api(url):
response = url_req.urlopen(url)
json_raw = response.read()
json_data = json.loads(json_raw)
return json_data
def get_places(types, pages):
location = str(NATAL_CENTER[0]) + "," + str(NATAL_CENTER[1])
mounted_url = ('%s'
'?location=%s'
'&radius=%s'
'&type=%s'
'&key=%s') % (API_NEARBY_SEARCH_URL, location, RADIUS, types, API_KEY)
results = []
next_page_token = None
if pages == None: pages = 1
for num_page in range(pages):
if num_page == 0:
api_response = request_api(mounted_url)
results = results + api_response['results']
else:
page_url = ('%s'
'?key=%s'
'&pagetoken=%s') % (API_NEARBY_SEARCH_URL, API_KEY, next_page_token)
api_response = request_api(str(page_url))
results += api_response['results']
if 'next_page_token' in api_response:
next_page_token = api_response['next_page_token']
else: break
time.sleep(1)
return results
def parse_place_to_list(place, type_name):
# Using name, place_id, lat, lng, rating, type_name
return [
place['name'],
place['place_id'],
place['geometry']['location']['lat'],
place['geometry']['location']['lng'],
type_name       
]
def mount_dataset():
data = []
for place_type in PLACES_TYPES:
type_name = place_type[0]
type_pages = place_type[1]
print("Getting into " + type_name)
result = get_places(type_name, type_pages)
result_parsed = list(map(lambda x: parse_place_to_list(x, type_name), result))
data += result_parsed
dataframe = pd.DataFrame(data, columns=['place_name', 'place_id', 'lat', 'lng', 'type'])
dataframe.to_csv('places.csv')
mount_dataset()

但脚本返回的数据帧为空如何解决并获得正确的数据集?

恐怕谷歌地图平台的服务条款禁止抓取和存储数据。

在推进实施之前,请先查看服务条款。第3.2.4段"防止滥用服务的限制"为

(a(无报废客户不会提取、导出或以其他方式抓取谷歌地图内容以供在服务之外使用。例如,客户不会:(i(在服务之外预取、索引、存储、重新共享或重新托管谷歌地图内容(ii(批量下载谷歌地图瓦片、街景图像、地理编码、方向、距离矩阵结果、道路信息、地点信息、海拔值和时区详细信息;(iii(复制并保存企业名称、地址或用户评论或(iv(将谷歌地图内容与文本到语音服务结合使用。

来源:https://cloud.google.com/maps-platform/terms/#3-许可

很抱歉,我听到了坏消息。

相关内容

  • 没有找到相关文章

最新更新