You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python抓取Leaflet地图所有弹窗内容?代码无响应求助

Leaflet地图弹窗文本提取代码问题分析与修复

核心问题总结

  • 未触发弹窗显示:Leaflet地图的弹窗不会自动展开,必须点击地图标记才会弹出。你的代码直接提取弹窗元素,但此时无任何弹窗打开,自然获取不到内容。
  • CSS选择器冗余脆弱:使用多层绝对DOM路径(#map > div.leaflet-map-pane > ...),DOM结构稍有变化就会失效,应改用.brand_container这类目标类名。
  • 循环逻辑无效:while(True)仅执行一次,找不到元素就直接break,完全没实现遍历标记的逻辑。
  • 未处理Cookie弹窗:注释了同意Cookie的代码,可能导致页面交互被遮挡,无法点击标记。
  • 缺少元素等待:未等待页面加载、标记渲染完成就执行查找,元素可能尚未存在。

修正后的代码

from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import pandas as pd

# 初始化浏览器
driver = webdriver.Firefox(executable_path="/Users/XXXXX/PycharmProjects/carte_pénurie_essence/geckodriver 2")
driver.get('https://penurie.mon-essence.fr/w/')
wait = WebDriverWait(driver, 15)

# 处理Cookie弹窗
try:
    wait.until(EC.element_to_be_clickable((By.ID, "didomi-notice-agree-button"))).click()
except:
    print("无Cookie弹窗或已自动处理")

# 等待地图标记加载完成
wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "div.leaflet-marker-icon")))

# 初始化存储列表
Nom_entreprise = []
Nom_entreprise_ville = []
Address_gps = []

# 获取所有地图标记
markers = driver.find_elements(By.CSS_SELECTOR, "div.leaflet-marker-icon")

# 遍历标记,点击并提取弹窗内容
for index, marker in enumerate(markers):
    try:
        # JS点击标记,避免元素遮挡问题
        driver.execute_script("arguments[0].click();", marker)
        # 等待弹窗加载
        wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "div.leaflet-popup-content-wrapper")))
        
        # 提取内容,使用简洁稳定的类选择器
        brand = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "div.brand_container"))).text
        station_name = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "div.station_name"))).text
        station_address = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "div.station_address"))).text
        
        # 添加到列表
        Nom_entreprise.append(brand)
        Nom_entreprise_ville.append(station_name)
        Address_gps.append(station_address)
        
        # 关闭弹窗,避免遮挡下一个标记
        close_btn = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.leaflet-popup-close-button")))
        driver.execute_script("arguments[0].click();", close_btn)
        
        print(f"已提取第{index+1}个站点信息")
    except Exception as e:
        print(f"处理第{index+1}个标记时出错: {str(e)}")
        continue

# 生成DataFrame并导出CSV
df = pd.DataFrame({
    "Nom_entreprise": Nom_entreprise,
    "Nom_entreprise_ville": Nom_entreprise_ville,
    "Address_gps": Address_gps
})

print(df)
df.to_csv("stations_en_pénurie.csv", index=False)

# 关闭浏览器
driver.quit()

代码改进说明

  • 新增Cookie弹窗处理,确保页面可正常交互
  • 使用WebDriverWait等待元素加载,避免未渲染完成导致的查找失败
  • 遍历所有地图标记,用JS点击解决元素遮挡无法点击的问题
  • 改用简洁的类选择器定位元素,提升代码稳定性
  • 提取后关闭弹窗,避免遮挡后续标记
  • 单个标记出错时跳过,不影响整体提取流程

内容的提问来源于stack exchange,提问作者TMB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 04:25:27