Skip to content

Instantly share code, notes, and snippets.

@nmolivo
Created February 8, 2018 08:17
Show Gist options
  • Save nmolivo/dd28c426b851e26cbd643fa4ec96188c to your computer and use it in GitHub Desktop.
Save nmolivo/dd28c426b851e26cbd643fa4ec96188c to your computer and use it in GitHub Desktop.
long-lat-selenium
#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