Python gets Gaode map POI - keyword search

The main content of this article is to use python to obtain points of interest (POIs) on the Gaode map.

Amap open platform: https://lbs.amap.com/
Download POI classification code and city code table
Search POI related documents: https://lbs.amap.com/api/webservice/guide/api/search

Foreword: Although I encountered a bug on the first day of using ChatGPT, which made me feel that ChatGPT is nothing more than that, but in the process of subsequent use, the experience is not bad, so using it as a tool to assist work or study can play a role A certain effect can improve efficiency, but it is simply unrealistic to completely rely on ChatGPT. So everyone uses ChatGPT reasonably.

Today I was lazy to use ChatGPT, and asked it to help me write a program that uses python to obtain the POI of the Gaode map. I tried it out. Although it is very simple, it works, so I exchanged feedback with it and improved the program step by step to meet my needs. .
Of course, this program is very simple, so it should be no problem for ChatGPT.

The main idea is to use python script combined with keyword search method to obtain POI data in a specific area from Gaode map

(It is relatively convenient to obtain POI data in a small area, but if the area is large, it is recommended to use the polygon search method , otherwise it is easy to obtain incomplete POI data)

code show as below:

import pandas as pd
import requests

# 高德地图 Web API 的 URL
url = "https://restapi.amap.com/v3/place/text"

# 输入需要搜索的关键字列表
keywords_list = ["公园", "银行"]

for keywords in keywords_list:
    # API 的参数
    params = {
    
    
        # 高德地图的API_KEY(即你的密匙)
        "key": "********************",
        "keywords": keywords,
        # 需要搜索的城市范围 可以是城市中文、citycode、adcode
        "city": "北京市",
        "offset": "20",
        "page": "1",
        "extensions": "all"
    }

    poi_list = []
    page = 1

    while True:
        # 发送 GET 请求并获取结果
        response = requests.get(url, params=params)
        result = response.json()
        pois = result["pois"]
        poi_list.extend(pois)

        # 如果返回的 POI 数据少于 20 条,说明已经获取完了所有数据,退出循环
        if len(pois) < 20:
            break

        # 将 page 的值加 1,以便获取下一页的数据
        page += 1
        params["page"] = str(page)

    # 将 POI 数据保存到 DataFrame 中
    df = pd.DataFrame(poi_list)

    # 将 DataFrame 中的数据保存到 CSV 文件中
    filename = f"{keywords}_poi.csv"
    df.to_csv(filename, index=False)

When using it, you only need to change keywords_list, "key", and "city"to what you need to search for 关键词, , 高德地图 Web 服务 API 密钥and 城市respectively.

If you want to search by the classification code of Gaode map , just change the parameter to parameter. Here is the sample code:typeskeywordstypes

import pandas as pd
import requests

# 高德地图 Web API 的 URL
url = "https://restapi.amap.com/v3/place/text"

# 输入需要搜索的分类代码
types = "060000|070000|080000"
# API 的参数
params = {
    
    
    # 高德地图的API_KEY(即你的密匙)
    "key": "*******************",
    "types": types,
    # 需要搜索的城市范围 可以是城市中文、citycode、adcode
    "city": "北京市",
    "offset": "20",
    "page": "1",
    "extensions": "all"
}

poi_list = []
page = 1

while True:
    # 发送 GET 请求并获取结果
    response = requests.get(url, params=params)
    result = response.json()
    pois = result["pois"]
    poi_list.extend(pois)

    # 如果返回的 POI 数据少于 20 条,说明已经获取完了所有数据,退出循环
    if len(pois) < 20:
        break

    # 将 page 的值加 1,以便获取下一页的数据
    page += 1
    params["page"] = str(page)

# 将 POI 数据保存到 DataFrame 中
df = pd.DataFrame(poi_list)

# 将 DataFrame 中的数据保存到 CSV 文件中
filename = f"{types}_poi.csv"
df.to_csv(filename, index=False)

When using it, you only need to change types, "key", "city"respectively to what you need to search for 分类代码, 高德地图 Web 服务 API 密钥and 城市.

Note: The maximum number of POIs obtained by the keyword search method does not exceed 200, so when you observe that your POI data is 190+, it means that the data has reached the limit of crawling, but the POI data has not been crawled completely . Therefore, it is recommended to use the polygon search method when the data is large !

This is a tutorial for crawling POIs using the polygon search method: Python Gets Gaode Map POI - Polygon Search

Guess you like

Origin blog.csdn.net/qq_43874102/article/details/129450767