简体   繁体   中英

Scraping longitude and latitude of a city from Google Maps

I want to extract Latitude and Longitude for a city from Google Maps. The flow should go like this

opening google maps URL Search for a city say 'New York'

Now I want to fetch the Latitude and Longitude of this city. Any help would be appreciated. Thanx in advance.

I could do it with context_click in selenium.

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

Don't forget to import these:

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

After exploring google maps and how a regular person would find the longitude and latitude i noticed the URL has the longitude and latitude within it so i would search for the location then pull the URL using driver.current_url or something like that(not familiar with python) then set it to a variable and use what ever function to trim it down and separate it so you only have the longitude and latitude in 2 separate variables. sorry this only breaks down the process rather then giving you a straight answer, if you have any other questions I would prefer you ask me here. https://discord.gg/fjNX5MuTVq

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM