-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFIBAA_webscraping.R
178 lines (142 loc) · 5.89 KB
/
FIBAA_webscraping.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
173
174
175
176
177
#seems to be working well
library(rvest)
library(tidyverse)
library(lubridate)
library(robotstxt)
library(XML)
library(RSelenium)
robotstxt::paths_allowed("http://www.fibaa.org/procedures-at-programme-level/prog-according-to-fibaa-quality-standards/accredited-programmes/?no_cache=1&L=1")
#binman::list_versions("chromedriver")
#if it tells you the versions don't match, run commented out line and see what version of chromedriver is available and put the
#latest as chromever argument.
driver <- rsDriver(browser=c("chrome"), chromever = "74.0.3729.6")
remDr <- driver[["client"]]
#Germany
remDr$navigate("http://www.fibaa.org/procedures-at-programme-level/prog-according-to-fibaa-quality-standards/accredited-programmes/?no_cache=1&L=1")
#find the elements which stand for region names
webElem <- remDr$findElements(using='class', "accordBland")
#loop through each region and click on it - they all get expanded
for (i in 1:length(webElem)) {
webElem[[i]]$highlightElement()
webElem[[i]]$clickElement()
}
Sys.sleep(2)
#in the expanded a there are business school names, this line grabs those elements
webElem <- remDr$findElements(using='class', "collegeName")
#his line takes the text
schools <- sapply(webElem, function(x){x$getElementText()})
#put them in a datame
schools <- as.data.frame(unique(unlist(schools)))
#adds columns
names(schools) <- "Name"
schools$Country <- "Germany"
#the same for pages for other countries!
#Austria
remDr$navigate("http://www.fibaa.org/procedures-at-programme-level/prog-according-to-fibaa-quality-standards/accredited-programmes/?no_cache=1&L=1")
country <- remDr$findElement(using = 'xpath', "//*/option[@value = 'http://www.fibaa.org/nc/en/procedures-at-programme-level/prog-according-to-fibaa-quality-standards/accredited-programmes/?menu=oesterreich']")
country$clickElement()
webElem <- remDr$findElements(using='class', "accordBland")
for (i in 1:length(webElem)) {
webElem[[i]]$highlightElement()
webElem[[i]]$clickElement()
}
Sys.sleep(2)
webElem <- remDr$findElements(using='class', "active")
schools1 <- sapply(webElem, function(x){x$getElementText()})
schools1 <- as.data.frame(unique(unlist(schools1)))
names(schools1) <- "Name"
schools1$Country <- "Austria"
schools1 <- schools1[2:12,]
#Switzerland
remDr$navigate("http://www.fibaa.org/procedures-at-programme-level/prog-according-to-fibaa-quality-standards/accredited-programmes/?no_cache=1&L=1")
country <- remDr$findElement(using = 'xpath', "//*/option[@value = 'http://www.fibaa.org/nc/en/procedures-at-programme-level/prog-according-to-fibaa-quality-standards/accredited-programmes/?menu=schweiz']")
country$clickElement()
webElem <- remDr$findElements(using='class', "accordBland")
for (i in 1:length(webElem)) {
webElem[[i]]$highlightElement()
webElem[[i]]$clickElement()
}
Sys.sleep(2)
webElem <- remDr$findElements(using='class', "active")
schools2 <- sapply(webElem, function(x){x$getElementText()})
schools2 <- as.data.frame(unique(unlist(schools2)))
names(schools2) <- "Name"
schools2$Country <- "Switzerland"
schools2 <- schools2[2:8,]
#Other
remDr$navigate("http://www.fibaa.org/procedures-at-programme-level/prog-according-to-fibaa-quality-standards/accredited-programmes/?no_cache=1&L=1")
country <- remDr$findElement(using = 'xpath', "//*/option[@value = 'http://www.fibaa.org/nc/en/procedures-at-programme-level/prog-according-to-fibaa-quality-standards/accredited-programmes.html?menu=weitere']")
country$clickElement()
webElem <- remDr$findElements(using='css', "li.fibaa")
for (i in 1:length(webElem)) {
webElem[[i]]$highlightElement()
webElem[[i]]$clickElement()
}
Sys.sleep(2)
#get a list of countries
webElem <- remDr$findElements(using='class', "active")
other_countries <- sapply(webElem, function(x){x$getElementText()})
other_countries <- as.data.frame(unique(unlist(other_countries)))
other_countries <- as.matrix(other_countries[2:25,])
webElem <- remDr$findElements(using='xpath', "//*[(@id = 'accordion')]//a")
schools3 <- sapply(webElem, function(x){x$getElementText()})
schools3 <- as.data.frame(unique(unlist(schools3)))
names(schools3) <- "Name"
schools3$Country <- NA
for (i in 1:(length(schools3$Name))) {
if (schools3$Name[i] %in% other_countries) {
schools3$Country[i] <- paste0(schools3$Name[i])
}
}
schools3 %>%
mutate(id = row_number(),
Country = ifelse(!is.na(Country),
paste(Country), paste(Country[lag(id)]))) ->schools3
if (sum(schools3$Country=="NA")!=0) {
schools3 %>%
mutate(id = row_number(),
Country = ifelse(Country!="NA",
paste(Country), paste(Country[lag(id)]))) ->schools3
}
if (sum(schools3$Country=="NA")!=0) {
schools3 %>%
mutate(id = row_number(),
Country = ifelse(Country!="NA",
paste(Country), paste(Country[lag(id)]))) ->schools3
}
if (sum(schools3$Country=="NA")!=0) {
schools3 %>%
mutate(id = row_number(),
Country = ifelse(Country!="NA",
paste(Country), paste(Country[lag(id)]))) ->schools3
}
if (sum(schools3$Country=="NA")!=0) {
schools3 %>%
mutate(id = row_number(),
Country = ifelse(Country!="NA",
paste(Country), paste(Country[lag(id)]))) ->schools3
}
if (sum(schools3$Country=="NA")!=0) {
schools3 %>%
mutate(id = row_number(),
Country = ifelse(Country!="NA",
paste(Country), paste(Country[lag(id)]))) ->schools3
}
schools3 <- na.omit(schools3)
schools3 <- schools3[,1:2]
for (i in 1:length(schools3$Name)) {
if (schools3$Name[i]== schools3$Country[i]) {
schools3[-i,] -> schools3
}
}
schools3$Name <- as.character(schools3$Name)
for (i in 1:length(schools3$Name)) {
if(nchar(schools3$Name[i])<1) {
schools3[-i,] -> schools3
}
}
schools <- rbind(schools, schools1, schools2, schools3)
write.csv(schools, file = paste0("files/FIBAA_accredited",today(),".csv"))
remDr$close()
rm(list=ls())
gc()