高德地图获取全国充电桩分布数据

key用自己申请的

url:https://restapi.amap.com/v3/place/text?city=%E5%8C%97%E4%BA%AC&offset=20&page=10&key=%EF%BC%9F&types=011100|011102|011103|073000|073001|073002&extensions=all

def handle_detail_and_save_to_csv(poi_list, city, province_):
    """
    处理POI详情信息并保存到CSV文件
    :param city:
    :param province_:
    :param poi_list: POI数据列表
    :return: None
    """
    # 配置Selenium WebDriver
    chrome_options = Options()
    chrome_options.add_argument("--headless")  # 无头模式
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)

    # Excel文件名
    xlsx_filename = '../Main/search/stations/province_data.xlsx'

    base_url = "https://www.amap.com/place/"

    # 检查Excel文件是否存在以及是否为空
    file_exists = False
    try:
        wb = openpyxl.load_workbook(xlsx_filename)
        sheet = wb.active
        file_exists = True
    except FileNotFoundError:
        wb = openpyxl.Workbook()
        sheet = wb.active

    # 如果文件为空或不存在,则写入表头
    if not file_exists:
        sheet.append(["省份", "城市", "名称", "地址", "电话", "经度", "纬度", "POI ID", "详情链接"])

    try:
        # 遍历 POI 列表,提取详细信息并写入 Excel
        for poi in poi_list:
            poi_id = poi.get('id')
            if not poi_id:
                continue  # 如果没有POI ID,则跳过此项

            # 获取POI详细页面
            driver.get(f"{base_url}{poi_id}")

            # 等待页面加载,假设页面有个元素用于标识详情加载完成
            driver.implicitly_wait(5)

            # 获取页面内容(你可以根据页面结构选择适当的获取方式)
            detail_json = driver.execute_script('return window.__INITIAL_STATE__')  # 假设页面数据存储在__INITIAL_STATE__
            print(detail_json)

            province1 = province_
            city1 = city

            # 从详细页面中提取必要的信息
            poi_name = poi.get('name', '无名称')
            poi_address = poi.get('address', '无地址')
            poi_phone = poi.get('tel', '无电话')
            poi_location = poi.get('location', '无经纬度').split(',')
            longitude = poi_location[0] if len(poi_location) > 0 else '无经度'
            latitude = poi_location[1] if len(poi_location) > 1 else '无纬度'

            # 只有在数据有效的情况下,才将数据写入Excel文件
            row_data = [province1, city1, poi_name, poi_address, poi_phone, longitude, latitude, poi_id,
                        f"{base_url}{poi_id}"]
            if all(row_data):  # 如果行数据不为空,则写入
                sheet.append(row_data)
                print(f"{poi_id} 详细信息已写入Excel文件.")
            else:
                print(f"{poi_id} 数据为空,跳过.")

    except Exception as e:
        print(f"发生错误: {e}")

    finally:
        # 保存 Excel 文件
        wb.save(xlsx_filename)
        print(f"数据已保存到 {xlsx_filename}")

        # 关闭浏览器
        driver.quit()

仅供参考学习,要源码的,后台滴我

Python爬取高德地图充电站点数据通常涉及网络爬虫技术,可以利用第三方库如`requests`发送HTTP请求获取网页内容,然后使用`BeautifulSoup`或`lxml`解析HTML结构,提取所需的数据高德地图API提供了相关的开放数据服务,如果你打算通过官方接口获取信息,你需要注册并获得相应的密钥。 以下是简单的步骤概述: 1. **安装必要的库**: - `requests`: 发送HTTP请求 - `beautifulsoup4` 或 `lxml`: 解析HTML ```bash pip install requests beautifulsoup4 # 或者如果使用lxml作为解析器 pip install lxml ``` 2. **设置高德地图API**: - 注册账号并申请开发者权限,获取API Key和城市编码等信息。 3. **编写Python脚本**: ```python import requests from bs4 import BeautifulSoup # 将你的API Key、城市编码替换这里 gmap_api_key = "your_api_key" city_code = "your_city_code" # 构造搜索URL url = f"https://restapi.amap.com/v3/place/search?city={city_code}&key={gmap_api_key}&output=json&extensions=all&searchType=around ChargingStation" response = requests.get(url) data = response.json() # 使用BeautifulSoup解析返回的JSON数据,提取充电站相关信息 stations = data['pois'] for station in stations: name = station['name'] # 充电站名称 location = station['location']['lat'], station['location']['lng'] # 经纬度坐标 print(f"名称:{name}, 地址:{location}") ``` 请注意,实际操作中,可能会遇到反爬机制或数据隐私策略限制,因此务必遵守高德地图的服务条款,并尊重数据所有权。另外,频繁的请求可能会被视为滥用,所以合理控制请求频率是很重要的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值