我想从 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/