Python3爬取高德地图POI

参考文档:https://lbs.amap.com/api/webservice/guide/api/search
思路:高德地图提供了4种查询POI信息的能力,其中包括关键字搜索、周边搜索、多边形搜索、ID查询四种筛选机制,本文采用的是多边形搜索。
产品介绍中提到“无论您指定多少个type,每次请求最多返回1000个POI信息,若场景需要获取更可能多的POI;建议您不要在type之中指定过多的类别,而是分多次请求从而得到更加准确的结果。”因此,本文的方法是将所要采集POI信息的区域(以济南市为例),首先利用arcgis软件对其创建渔网(图1),然后通过渔网对角坐标获取工具(图2),百度网盘免费分享给大家,得到每个渔网对角坐标(左下和右上)(图3)在这里插入图片描述
图1

在这里插入图片描述
图2
在这里插入图片描述
图3

接下来就是写代码的过程,话不多说,代码如下:

import requests
import json
from pymongo import MongoClient
import time

client = MongoClient('localhost',27017)
db = client.POI_Jinanshi
collection = db.table_1
polygon_list = list()


with open("jn2.txt", 'r', encoding='UTF-8') as txt_file:
    for each_line in txt_file:
        if each_line != "" and each_line != "\n":
            fields = each_line.split("\n")
            polygon = fields[0]
            polygon_list.append(polygon)
    txt_file.close()

def getjson(polygon, page):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36'
    }
    pa = {
        'key': 'xxxxxxxxxxxxxxxxxxxxxx', #从控制台申请
        'polygon': polygon,
        'types':'970000|990000',    #不要过多
        'city':'0531',
        'offset': 20,
        'page': page,
        'extensions': 'all',
        'output': 'JSON'
    }
    r = requests.get('https://restapi.amap.com/v3/place/polygon?', params=pa, headers=headers)
    decodejson = json.loads(r.text)
    return decodejson

for each_polygon in polygon_list:
    not_last_page = True
    page = 1
    while not_last_page:
        decodejson = getjson(each_polygon, page)
        print(decodejson)
        count = decodejson['count']
        print(each_polygon, page)
        if decodejson['pois']:
            for eachone in decodejson['pois']:
                try:
                    name = eachone['name']          #POI名称
                except:
                    name = None
                try:
                    types = eachone['type']           #POI所属类别
                except:
                    types = None
                try:
                    address = eachone['address']    #POI地址
                except:
                    address = None
                try:
                    loaction = eachone['location']   #POI坐标
                except:
                    loaction = None
                try:
                    city = eachone['cityname']    #城市
                except:
                    city = None
                try:
                    county = eachone['adname']   #区县
                except:
                    county = None
                data={
                    'name':name,
                    'types':types,
                    'address':address,
                    'location':loaction,
                    'city':city,
                    'county':county,
                    'count':count,
                    'polygon':each_polygon
                }
                collection.insert_one(data)
                time.sleep(0.2)
            page += 1
        else:
            not_last_page = False

采集的结果如下:
在这里插入图片描述
渔网对角坐标获取工具:
链接:链接:https://pan.baidu.com/s/11nwWHDf75fSVTxs323opFQ
提取码:rduv

谢谢浏览!

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页