使用Python和Google Places API收集地点

问题描述 投票:0回答:1

我想用latlong(0.507068,101.447777)收集我的城市北干巴鲁周围的地点,然后将其转换为数据集。数据集(它包含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()

但是脚本返回了Empty DataFrame。如何解决并获得正确的数据集?

python google-maps dataframe google-api google-places-api
1个回答
1
投票

[恐怕Google Maps Platform服务条款禁止抓取和存储数据。

在实施之前,请先阅读服务条款。第3.2.4节“禁止滥用服务的限制”内容为:

(a)无刮擦。客户不会提取,导出或以其他方式刮擦Google Maps Content,以在服务之外使用。例如,客户不会:(i)在服务之外预取,索引,存储,转发或重新托管Google Maps内容;(ii)批量下载Google Maps磁贴,街景图像,地理编码,路线,距离矩阵结果,道路信息,地点信息高程值和时区详细信息; (iii)复制并保存公司名称,地址或用户评论;或(iv)将Google Maps Content与文本语音转换服务一起使用。

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

很抱歉成为坏消息的承担者。

© www.soinside.com 2019 - 2024. All rights reserved.