[英]Scrape Google Map coordinates on Airbnb
I'm using selenium to scrape some info on airbnb.我正在使用 selenium 来获取有关 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:这是 HTML 的样子:
<a style="display: inline;" target="_blank" rel="noopener" title="Ouvrir cette zone dans Google Maps (dans une nouvelle fenêtre)" aria-label="Ouvrir cette zone dans Google Maps (dans une nouvelle fenêtre)" href="https://maps.google.com/maps?ll=23.1345,-82.3543&z=14&t=m&hl=fr&gl=CA&mapclient=apiv3">
If I try to print the href, I get this result:如果我尝试打印 href,我会得到以下结果:
/sitemaps/v2
You don't need to convert the href
to a string since it is alread a string.您不需要将
href
转换为字符串,因为它已经是字符串。 Moreover, I suggest you to use the new notation By.
此外,我建议您使用新的符号
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])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.