Python 爬取 Google Map POI

Google Map 的 API文档:https://developers.google.com/maps/documentation/places/web-service/search#PlaceSearchRequests

这里使用的其中的 Place Search Requests 接口。需要先注册谷歌地图的开发者平台获取 key。

原理上是按照一定的格点来请求周围的POI。每次请求会返回大约十几个POI点信息,类似于一种采样,如果在POI比较密集的地区,格网比较大会漏过许多POI。

下面这段代码爬取的是曼谷城区的部分POI

# 从谷歌API获取POI
import urllib.request 
from urllib.parse import quote 
import string
import json
import codecs
import numpy

# 参数
lonRange = [100.30, 100.60]  # the range of longitude 经度的范围 
latRange = [13.50, 14]  # the range of latitude 纬度的范围  
lonDivision = 0.005 # 分块查询,每格约0.4km
latDivision = 0.005 # 分块查询,每格约0.4km
radius = 500 # 查询参数 半径 500m
TIMEOUT = 30
outfile = "output.csv"

#   Google Key
googleKey = "在这里填你自己的key"

#restaurant_j = json_format(res_test)
print('开始爬取')
print('共有'+str(((lonRange[1]-lonRange[0])/lonDivision+1)*((latRange[1]-latRange[0])/latDivision+1))+'次请求')
count = 0
countLine = 0

place_id_list = []
csvFile=codecs.open(outfile,'a','utf-8')
csvFile.write('lat,lon,types\n')

for lon in numpy.arange(lonRange[0], lonRange[1], lonDivision):
    print('已进行'+str(count)+'次请求,得到'+str(countLine)+'条有效信息')
    for lat in numpy.arange(latRange[0], latRange[1], latDivision):
        print('已进行'+str(count)+'次请求,得到'+str(countLine)+'条有效信息')
        #   发请求
        latlon = str(lat)+','+str(lon)
        basic_url = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?key={0}&location={1}&radius={2}'  
        url = basic_url.format(googleKey,latlon,radius)
        url = quote(url, safe = string.printable)
        req = urllib.request.urlopen(url,timeout=TIMEOUT)
        response = req.read().decode('utf-8')
        responseJSON = json.loads(response)
        for item in responseJSON['results']:
            #对每个POI
            place_id = item['place_id']
            types = item['types']
            #如果id不在已有的list里
            if not place_id in place_id_list:
                #如果类型中有point_of_interest
                if "point_of_interest" in types:
                    place_id_list.append(place_id)
                    line = str(item['geometry']['location']['lat']) + ',' + str(item['geometry']['location']['lng'])
                    for type in types:
                        line = line + ',' + str(type)
                    csvFile.write(line + '\n')
                    countLine = countLine + 1
        count = count + 1
csvFile.close()
print('结束')

爬取结果使用 ArcGIS Pro 可视化如下:

POI可视化

评论 31
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值