I am trying to scrape some information from the following:
url = "https://www.fotocasa.es/indice-precio-vivienda/barcelona-capital/dreta-de-l-eixample"
preciosComAlq = url %>%
read_html()
I can get the subtitle information using:
preciosComAlq %>%
html_elements(".t-panel-container") %>%
html_elements(".t-panel.comprar") %>%
#html_nodes("h3")
html_elements(".t-panel_subtitle")
Which gives:
{xml_nodeset (5)}
[1] <h3 class="t-panel_subtitle">\r\n Precio m² por número de habitaciones\r\n </h3>
[2] <h3 class="t-panel_subtitle">\r\n Valor medio de la vivienda por tamaño\r\n </h3>
[3] <h3 class="t-panel_subtitle">Precio m² por características</h3>
[4] <h3 class="t-panel_subtitle">Ofertas activas de comprar</h3>
[5] <h3 class="t-panel_subtitle">Características más comunes</h3>
But I want to collect the information below each subtitle.
For example Precio m² por características I want the following:
6.345 €/m²
Precio con terraza
lift
6.654 €/m²
Con ascensor
amuebla
7.735 €/m²
Amueblado
parking
7.133 €/m²
Con parking
I can do something like:
bind_cols(
preciosComAlq %>%
html_elements(".t-panel-container") %>%
html_elements(".t-panel.comprar") %>%
html_elements(".description-wrap") %>%
html_elements(".description-item_inner-wr") %>%
html_elements(".description-item_semibold") %>%
html_text() %>%
trimws(),
preciosComAlq %>%
html_elements(".t-panel-container") %>%
html_elements(".t-panel.comprar") %>%
html_elements(".description-wrap") %>%
html_elements(".description-item_inner-wr") %>%
html_elements(".description-item_regular") %>%
html_text() %>%
trimws()
)
Which gives:
# A tibble: 12 × 2
...1 ...2
<chr> <chr>
1 9.538 €/m² Precio de estudio o 1 habitación
2 6.176 €/m² Precio de 3 habitaciones
3 7.273 €/m² Precio de 2 habitaciones
4 5.813 €/m² Precio de más de 3 habitaciones
5 6.345 €/m² Precio con terraza
6 6.654 €/m² Con ascensor
7 7.735 €/m² Amueblado
8 7.133 €/m² Con parking
9 140m² Metros cuadrados
10 Piso Tipo de inmueble
11 3 Número de habitaciones
12 5 Planta del inmueble
However, I don't know what section each item/row corresponds to.
From looking at the webpage I see that
9.538 €/m² Precio de estudio o 1 habitación
Correspond to Precio m² por número de habitaciones.
But I would also like to link:
5 6.345 €/m² Precio con terraza
6 6.654 €/m² Con ascensor
7 7.735 €/m² Amueblado
8 7.133 €/m² Con parking
With its correct corresponding subtitle = Precio m² por características. So the expected output would be something like the following, a new column with the subtitle heading.
# A tibble: 12 × 2
...1 ...2
<chr> <chr>
1 9.538 €/m² Precio de estudio o 1 habitación Precio m² por número de habitaciones
2 6.176 €/m² Precio de 3 habitaciones Precio m² por número de habitaciones
3 7.273 €/m² Precio de 2 habitaciones Precio m² por número de habitaciones
4 5.813 €/m² Precio de más de 3 habitaciones Precio m² por número de habitaciones
5 6.345 €/m² Precio con terraza Precio m² por características
6 6.654 €/m² Con ascensor Precio m² por características
7 7.735 €/m² Amueblado Precio m² por características
8 7.133 €/m² Con parking Precio m² por características
9 140m² Metros cuadrados Características más comunes
10 Piso Tipo de inmueble Características más comunes
11 3 Número de habitaciones Características más comunes
12 5 Planta del inmueble Características más comunes