from selenium import webdriver
def scrape_coordinates(city): # Initialize the Chrome driver driver = webdriver.Chrome() driver.maximize_window()
# Open Google Maps
driver.get("https://maps.google.com")
# Find the search bar and enter the city
search_bar = driver.find_element_by_css_selector("input[aria-label='Search Google Maps']")
search_bar.send_keys(city)
search_bar.submit()
# Wait for the map to load and find the coordinates element
driver.implicitly_wait(5)
coordinates_element = driver.find_element_by_css_selector(".QSFF4-text")
# Get the coordinates text
coordinates_text = coordinates_element.get_attribute("textContent")
latitude, longitude = coordinates_text.split(",")
# Close the browser
driver.quit()
return latitude.strip(), longitude.strip()
Get user input
city_input = input("Enter the city name to search for coordinates on Google Maps: ")
Scrape coordinates
latitude, longitude = scrape_coordinates(city_input)
print(f"Coordinates for {city_input}: Latitude - {latitude}, Longitude - {longitude}")