python - 从 Google map 中抓取城市的经度和纬度

标签 python selenium

我想从 Google map 中提取某个城市的纬度和经度。流程应该是这样的

打开谷歌地图 URL 搜索名为“纽约”的城市

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

最佳答案

我可以使用 selenium 中的 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)

O/P:

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

关于python - 从 Google map 中抓取城市的经度和纬度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67851013/

相关文章:

javascript - 无法使用 Selenium 在自定义滚动条上滚动

python - 断管错误 selenium webdriver,当命令之间存在间隙时?

python - XSLT 与 Python 的 lxml : Forbidden Variable?

maven - 如何使用 Selenium 2.0 和 Maven 运行 HTML Selenese 测试?

Python Selenium Chrome 网络驱动程序

java - 无法创建 ChromeDriver 对象

python - 无法使用 python 将 UDP 数据包发送到本地端口,根据 WireShark 校验和失败

Python 日志记录配置文件

python - Pandas 虫? : Mean of an grouped-by int64 column stays as int64 in some circumstances

python - 使用 SQLAlchemy session 处理 Flask 和并发问题