-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape_credit_ratings.R
63 lines (40 loc) · 1.66 KB
/
scrape_credit_ratings.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
library(dplyr)
library(rvest)
library(data.table)
###########
setwd() # set your working directory
###########
# country list
ratingpage <- "https://tradingeconomics.com/country-list/rating"
rating <- read_html(ratingpage)
chart <- rating %>%
rvest::html_nodes('body') %>% # search body of html
xml2::xml_find_all("//table[contains(@class, 'table')]") # get all table elements
countries_table <- html_table(chart)[[1]]
countries_df <- data.frame(countries_table)
####################
# download data for all countries
# list of countries from table above
countries_list <- countries_df[,1]
# loop
country_ratings <- list()
for(i in 1:length(countries_list)){
countryname <- gsub(" ", "-", countries_list[i]) # replace spaces in country names with dashes
ratingpage <- paste0("https://tradingeconomics.com/", countryname, "/rating")
tryCatch({
rating <- read_html(ratingpage)
chart <- rating %>%
rvest::html_nodes('body') %>%
xml2::xml_find_all("//table[contains(@class, 'table')]")
ratings_table <- html_table(chart)[[1]]
country_ratings[[i]] <- data.frame(cbind(countryname, ratings_table))
print(countryname)
}, error = function(e){cat("ERROR with ", countries_list[i], ":",conditionMessage(e), "\n")}) # print error message if code fails
}
# unlist into one data frame
country_ratings_df <- rbindlist(country_ratings)
colnames(country_ratings_df) <- c("country", "agency", "rating", "outlook", "date")
country_ratings_df$date <- as.Date(country_ratings_df$date, "%B %d %Y")
country_ratings <- country_ratings_df
# save
save(country_ratings, file = "ratings.Rdata")