-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilm_and_time_scraper.R
159 lines (141 loc) · 4.37 KB
/
film_and_time_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
#### LOAD PACKAGES ####
library(rvest)
library(magrittr)
library(dplyr)
library(tidyr)
library(purrr)
library(lubridate)
library(readr)
library(stringr)
library(stringi)
#### LOAD URL AND READ HTML ####
html_data <- read_html("https://www.golem.es/golem/vangolem")
#### HANDS ON SCRAPING ####
## Get the films titles
films_url <- tibble(
films = html_data %>%
html_nodes("span.txtNegXL a") %>%
html_text2(),
url = html_data %>%
html_nodes(".m5") %>%
html_attr("href")
)
films_url <- films_url %>%
mutate(
url = paste0("https://www.golem.es", url)
)
films <- films_url %>%
select(-url)
## Create the dataframe to store the time information
for (i in 1:4){
new <- rep(i, nrow(films))
films[ , ncol(films)+1] <- new
colnames(films)[ncol(films)] <- paste0("time", i)
}
## Fill the time values
z <- 0
# The i iterator is for the films
for (i in seq(1, 3+(nrow(films)-1)*2, 2)){
# print(paste0("i es: ", i))
z <- z + 1
if (z > nrow(films)) break
# print(paste0("z es: ", z))
# The j iterator is for the film times
for (j in 2:5){
# print(paste0("j es: ", j))
css_tag <- paste0(".tabContenido table:nth-child(",i,") table table table td:nth-child(",j-1,") td .CajaVentasSup .horaXXXL .horaXXXL")
films[[j]][[z]] <- if (length(
html_data %>%
html_nodes(css_tag) %>%
html_text2()
)
) html_data %>%
html_nodes(css_tag) %>%
html_text2() else NA
}
}
## Add a date column
films <- films %>%
mutate(date = Sys.Date()) %>%
relocate(.before = films, date)
prueba <- left_join(films_url, films)
mapeador <- tibble(
html_result = map(prueba$url,
~ {
Sys.sleep(2)
.x %>%
read_html()
}),
url = prueba$url
)
results_by_film_url <- tibble(
url = mapeador$url,
cas_titulo = map(mapeador$html_result,
~ .x %>%
html_nodes(".txtNegL tr:nth-child(2) .txtLectura:nth-child(1)") %>%
html_text2()
),
dato_titulo = map(mapeador$html_result,
~ .x %>%
html_nodes(".txtNegL tr:nth-child(2) .txtLectura:nth-child(2)") %>%
html_text2()
),
cas_director = map(mapeador$html_result,
~ .x %>%
html_nodes(".txtNegL tr:nth-child(3) .txtLectura:nth-child(1)") %>%
html_text2()
),
dato_director = map(mapeador$html_result,
~ .x %>%
html_nodes(".txtNegL tr:nth-child(3) .txtLectura:nth-child(2)") %>%
html_text2()
),
cas_length = map(mapeador$html_result,
~ .x %>%
html_nodes(".txtNegL tr:nth-child(4) .txtLectura:nth-child(1)") %>%
html_text2()
),
dato_length = map(mapeador$html_result,
~ .x %>%
html_nodes(".txtNegL tr:nth-child(4) .txtLectura:nth-child(2)") %>%
html_text2()
),
cas_country = map(mapeador$html_result,
~ .x %>%
html_nodes(".txtNegL tr:nth-child(5) .txtLectura:nth-child(1)") %>%
html_text2()
),
dato_country = map(mapeador$html_result,
~ .x %>%
html_nodes(".txtNegL tr:nth-child(5) .txtLectura:nth-child(2)") %>%
html_text2()
)
)
results_by_film_url <- results_by_film_url %>%
unnest(2:9)
df <- left_join(prueba, results_by_film_url)
df <- df %>%
pivot_longer(
cols = 8:15,
names_to = "casillas",
names_prefix = "cas_",
values_to = "datos"
) %>%
filter(
str_detect(casillas, "dato_")
) %>%
mutate(
casillas = str_remove(casillas, "dato_")
) %>%
pivot_wider(names_from = casillas,
values_from = datos)
df <- df %>%
dplyr::rename(
original_title = titulo
) %>%
relocate(
date, .before = films
) %>%
select(-url)
#### APPEND DATA DAY TO DAY TO A .CSV FILE ####
write.table(df, "data/films_van_golem.csv", fileEncoding = "UTF-8", sep = ",", row.names = FALSE, col.names = F, append = T)