-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.R
172 lines (138 loc) · 4.3 KB
/
scraper.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# General-purpose data wrangling
library(tidyverse)
# Parsing of HTML/XML files
library(rvest)
# String manipulation
library(stringr)
# get last page function
get_last_page <- function(html) {
pages_data <- html %>%
html_nodes(".page") %>%
html_text() %>%
as.numeric()
pages_data[length(pages_data)]
}
# extract information from page
# get price
get_price <- function(html) {
price <- html %>%
html_nodes(".offer-price__number") %>%
html_text() %>%
str_trim() %>%
unlist() %>%
`[[`(1) %>%
str_extract_all(., "\\d+") %>%
`[[`(1)
pasted_price <- paste0(price[1], price[2]) %>%
as.numeric()
return(pasted_price)
}
get_params_label <- function(html) {
html %>%
html_nodes(".offer-params__label") %>%
html_text() %>%
str_trim()
}
get_params_value <- function(html) {
html %>%
html_nodes(".offer-params__value") %>%
html_text() %>%
str_trim()
}
get_data_table <- function(html) {
# create table with basic info
df <- data.frame(x = get_params_label(html),
y = get_params_value(html),
stringsAsFactors = FALSE)
df <- setNames(data.frame(t(df[, -1])), df[, 1])
df <- df %>% mutate(Cena = get_price(html))
return(df)
}
get_data_from_url <- function(url) {
pb$tick()$print()
html <- try(read_html(url), silent = TRUE)
if (!inherits(html, "try-error")) {
get_data_table(html)
}
}
scrape_table <- function(url) {
# Read first page
first_page <- read_html(url)
# Extract number of pages
latest_page_number <- get_last_page(first_page)
# generate list of pages
list_of_pages <- str_c(url, "&page=", 1:latest_page_number)
# get pages
pages <- lapply(list_of_pages, read_html)
# generate list of offers
list_of_offers <- lapply(pages, function(page) {
page %>%
html_nodes(".adListingItem") %>%
html_attrs()
})
list_of_offers <- lapply(list_of_offers, function(lista) {
lapply(lista, function(offer) {
offer %>%
as.list() %>%
purrr::pluck("data-href")
})
}) %>%
unlist()
pb <<- progress_estimated(length(list_of_offers))
# Apply function to extract info and bind it
list_of_offers %>%
map(~get_data_from_url(.)) %>%
bind_rows()
}
lexus <- scrape_table("https://www.otomoto.pl/osobowe/gs/?search%5Bfilter_enum_fuel_type%5D=petrol&search%5Bnew_used%5D=on")
# plot price vs year of production
lexus %>%
mutate(`Rok produkcji` = as.numeric(`Rok produkcji`)) %>%
ggplot(.,
aes(x = `Rok produkcji`,
y = Cena)) +
geom_point() +
geom_smooth() +
theme_bw() +
scale_x_continuous(breaks = seq(min(lexus$`Rok produkcji`), max(lexus$`Rok produkcji`, by = 1))) +
labs(title = "Cena vs rok produkcji dla Lexus GS na otomoto.pl")
url <- "https://www.otomoto.pl/osobowe/mazda/6/od-2016/?search%5Bfilter_float_year%3Ato%5D=2017&search%5Bfilter_enum_fuel_type%5D%5B0%5D=petrol&search%5Bfilter_enum_gearbox%5D%5B0%5D=automatic&search%5Bfilter_enum_gearbox%5D%5B1%5D=cvt&search%5Bfilter_enum_gearbox%5D%5B2%5D=dual-clutch&search%5Bfilter_enum_gearbox%5D%5B3%5D=semi-automatic&search%5Border%5D=created_at%3Adesc&search%5Bbrand_program_id%5D%5B0%5D=&search%5Bcountry%5D="
mazda <- scrape_table(url)
mazda %>%
mutate(`Rok produkcji` = as.numeric(`Rok produkcji`)) %>%
ggplot(.,
aes(x = `Rok produkcji`,
y = Cena)) +
geom_boxplot() +
theme_bw()
mazda %>%
filter(Typ == "Sedan") %>%
summarize(min = min(Cena),
max = max(Cena),
median = median(Cena))
# change przebieg to numeric
change_przebieg_to_numeric <- function(df) {
km <- sapply(str_extract_all(df$Przebieg, "\\d+"), function(x) {
as.numeric(paste0(x[1], x[2]))
})
df$Przebieg <- km
df
}
# change and round pojemnosc to numeric
change_pojemnosc_to_numeric <- function(df) {
pojemnosc <- sapply(str_extract_all(df$`Pojemność skokowa`, "\\d+"), function(x) {
as.numeric(paste0(x[1], x[2]))
})
pojemnosc <- round(pojemnosc, -2)
df$`Pojemność skokowa` <- pojemnosc
df
}
mazda <- change_przebieg_to_numeric(mazda)
mazda <- change_pojemnosc_to_numeric(mazda)
ggplot(mazda %>%
filter(Przebieg < 60000,
Cena > 75000),
aes(x = Przebieg,
y = Cena)) +
geom_point() +
facet_wrap(~`Pojemność skokowa`)