Home > Blockchain >  Scrape Google Map coordinates on Airbnb
Scrape Google Map coordinates on Airbnb

Time:05-28

I'm using selenium to scrape some info on airbnb. However, I cant find a way to scrape the coordinates.

This is a simple version of my code:

from selenium import webdriver
from bs4 import BeautifulSoup

driver.get("https://fr.airbnb.ca/rooms/19608536?federated_search_id=686e8698-17a9-4d4d-bbba-100072721de7&source_impression_id=p3_1652976246_32j783N8ZqTYE1DA")

s = str(driver.find_element_by_xpath(
        "/html/body/div[5]/div/div/div[1]/div/div/div[1]/div/div/div/div/div[1]/main/div/div[1]
         /div[5]/div/div/div/div[2]/div/section/div[3]/div[3]/div[4]/div/div/div[14]
         /div/a").get_attribute("href")) #location of the href ("https://maps.google.com/maps?ll=23.1345,-82.3543&z=14&t=m&hl=fr&gl=CA&mapclient=apiv3 ")

coordo = re.search('maps?ll=(.*)&z=', s).group(1) #extract the coordinates 
lat = coordo.split(",")[0]
lng = coordo.split(",")[1]

This is what the HTML look like:

<a style="display: inline;" target="_blank" rel="noopener" title="Ouvrir cette zone dans Google&nbsp;Maps (dans une nouvelle fenêtre)" aria-label="Ouvrir cette zone dans Google&nbsp;Maps (dans une nouvelle fenêtre)" href="https://maps.google.com/maps?ll=23.1345,-82.3543&amp;z=14&amp;t=m&amp;hl=fr&amp;gl=CA&amp;mapclient=apiv3">

If I try to print the href, I get this result:

/sitemaps/v2

CodePudding user response:

You don't need to convert the href to a string since it is alread a string. Moreover, I suggest you to use the new notation By. since the one you are using is deprecated.

from selenium.webdriver.common.by import By

s = driver.find_element(By.XPATH, '/html/...').get_attribute('href')
lat = float(s.split(',')[0].split('=')[1])
lng = float(s.split(',')[1].split('&')[0])
  • Related