Парсим Avito

Зарманбетов Ахмед


Пакеты

# install.packages(с('rvest', 'dplyr', 'ggplot2', 'tibble', 'stringr'))
library(rvest)
library(dplyr)
library(ggplot2)
library(tibble)
library(stringr)

Парсим первую страничку

url <- 'https://www.avito.ru/moskva?p=1&q=Гитара'
avito <- read_html(url)

Находим количество страниц.

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=Гитара')

Берем первую страничку и парсим ее.

link <- links[1]
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()

Парсим все страницы

Задаем 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)
}