首页 > 解决方案 > 从谷歌地图中抓取城市的经度和纬度

问题描述

我想从谷歌地图中提取一个城市的纬度和经度。流程应该是这样的

打开谷歌地图 URL 搜索一个城市说“纽约”

现在我想获取这个城市的纬度和经度。任何帮助,将不胜感激。提前谢谢。

标签: pythonselenium

解决方案


我可以用context_click硒来做。

wait = WebDriverWait(driver, 10)
driver.get("https://www.google.com/maps")
wait.until(EC.element_to_be_clickable((By.ID, "searchboxinput"))).send_keys("New York")
wait.until(EC.element_to_be_clickable((By.ID, "searchbox-searchbutton"))).click()
sleep(5)
ActionChains(driver).move_to_element(driver.find_element_by_xpath("//html/body")).context_click().perform()
print(wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "ul[role='menu']>li div div[class*='text']:nth-of-type(1)"))).text)

输出/输出:

40.69715, -74.25987

不要忘记导入这些:

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

推荐阅读