I am webscraping this website, I am having troubles when I try to rbind all the columns in the datasets. it gives me error. it says because different number of rows between columns in the dataset, for example 25 elements in price and 24 in description.
{if(length(.) == 0) NA else .}
I tried to put the piece of code above to put NAs when the webscrape program doesn't find values but it looks it doesn't work, I leave the full code below.
urls <- sprintf("https://www.immobiliare.it/vendita-case/milano/?pag=%d", 1:7)
case <- data.frame()
for (i in urls){
page <- read_html(i)
Scrape <- page %>% html_nodes(xpath= "//ul[@class='nd-list in-realEstateResults']") %>%
purrr::map_df(~list(description= html_nodes(.x, xpath= "//a[@class='in-card__title']") %>% html_text() %>% length() %>% {if(length(.) == 0) NA else .}, #Returns NA for missing data
price= html_nodes(.x, xpath= "//li[@class='nd-list__item in-feat__item in-feat__item--main in-realEstateListCard__features--main']") %>% html_text(trim = TRUE) %>% {if(length(.) == 0) NA else .},
rooms = html_nodes(.x,xpath= "//li[@aria-label='locali']") %>% html_text(trim = TRUE) %>% {if(length(.) == 0) NA else .},
area= html_nodes(.x,xpath= "//li[@aria-label='superficie']") %>% html_text(trim = TRUE) %>% {if(length(.) == 0) NA else .}))
temp <- data.frame(Scrape)
case <- rbind(temp, case)
print(paste("Page:",i))
}
any suggestions? let me know if you have any questions