Created
February 8, 2018 08:17
-
-
Save nmolivo/dd28c426b851e26cbd643fa4ec96188c to your computer and use it in GitHub Desktop.
long-lat-selenium
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#function to webscrape | |
driver = webdriver.Firefox() | |
wait = WebDriverWait(driver, 30) | |
coords = [] | |
driver.get('https://www.google.com/maps') | |
for school in schools: | |
searchbox = wait.until(EC.presence_of_element_located((By.ID, 'searchboxinput'))) | |
searchbox.clear() | |
searchbox.send_keys(school + ' school Washington DC') | |
driver.find_element_by_id('searchbox-searchbutton').click() | |
sleep(6) | |
url = driver.current_url | |
tries = 0 | |
while '@' not in url: | |
tries += 1 | |
sleep(1) | |
url = driver.current_url | |
if tries == 5: #try five times to give the url the chance to resolve | |
break | |
try: | |
long_lat = url.split('@')[1].split(',')[:2] | |
coords.append((school, long_lat[0], long_lat[1])) | |
except: | |
coords.append((school, None, None)) | |
driver.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment