Находим количество страниц.
span <- avito %>% html_nodes(xpath = '/html/body/div[1]/div[2]/div[2]/div[3]/div[4]/div[1]') %>%
html_nodes(xpath = 'span')
count <- span[length(span) - 1] %>%
html_text() %>%
as.numeric()
links <- paste0('https://www.avito.ru/moskva?p=', 1:count, '&q=Гитара')
Берем первую страничку и парсим ее.
Задаем for
на все страницы. А также ставим Sys.sleep
, чтобы нас не забанили))
p <- c()
for(i in 1:length(links)){
link <- links[i]
link_html <- read_html(link)
p_link <- link_html %>%
html_nodes(xpath = "//span[@class ='price ']") %>%
html_text() %>%
str_replace_all('\n', '') %>%
str_replace_all('\u20bd', '') %>%
str_replace_all(' ', '') %>%
as.numeric()
p <- c(p, p_link)
Sys.sleep(1)
}