I have my code below which is scraping some information from a website.
for (i in Player_data$P_URL) {
WS2 <- read_html(i)
MarketValue <- WS2 %>% html_nodes(".dataMarktwert a") %>% html_text()
%>% as.character()
Age <- WS2 %>% html_nodes(".dataDaten:nth-child(1) p:nth-child(1)
.dataValue") %>% html_text() %>% as.character()
Player <- WS2 %>% html_nodes("h1") %>% html_text() %>% as.character()
Minutes <- WS2 %>% html_node(".odd:nth-child(2) .zentriert:nth-
child(7)") %>% html_text() %>% as.character()
if (identical(Minutes, character(0))) {
Minutes = 0
}
if (identical(MarketValue, character(0))) {
MarketValue = 0
}
if (identical(Position, character(0))) {
Position = NA
}
temp2 <- data.frame(Player,MarketValue,Age,Minutes)
Player_Values <- rbind(Player_Values,temp2)
}
However if the Player is a Goalkeeper the Minutes would be scarped using:
Minutes <- WS2 %>% html_node(".odd:nth-child(2) .zentriert:nth-
child(6)") %>% html_text() %>% as.character()
I would like to automate this process in my loop so that if a GoalKeeper is identified in the HTML then the loop would scrape the minutes using the code line above rather than the one inside my loop already.
For the HTML to identify if a player is a Goalkeeper I think the line of code would be this:
WS2 %>% html_nodes("#yw1 .inline-table tr+ tr td") %>% html_text() %>%
as.character()
But I am unsure of how to incorporate this into my loop.
Thank you for any help in advance
Aucun commentaire:
Enregistrer un commentaire