J'essaie d'utiliser rvest pour extraire les informations de pays ISO de wikipedia (y compris les liens d'une autre page). Je ne peux pas trouver un moyen d'obtenir correctement les liens (attribut href) sans inclure le nom (j'ai essayé la fonction de chaîne xpath cela provoque une erreur). C'est assez facile à gérer - et explicite.R - Grattage de pages Web - Problème lors de l'obtention des valeurs d'attribut en utilisant rvest
Toute aide appréciée!
library(rvest)
library(dplyr)
searchPage <- read_html("https://en.wikipedia.org/wiki/ISO_3166-2")
nodes <- html_node(searchPage, xpath = '(//h2[(span/@id = "Current_codes")]/following-sibling::table)[1]')
codes <- html_nodes(nodes, xpath = 'tr/td[1]/a/text()')
names <- html_nodes(nodes, xpath = 'tr/td[2]//a[@title]/text()')
#Following brings back data but attribute name as well
links <- html_nodes(nodes, xpath = 'tr/td[2]//a[@title]/@href')
#Following returns nothing
links2 <- html_nodes(nodes, xpath = 'tr/td[2]//a[@title]/@href/text()')
#Following Errors
links3 <- html_nodes(nodes, xpath = 'string(tr/td[2]//a[@title]/@href)')
#Following Errors
links4 <- sapply(nodes, function(x) { x %>% read_html() %>% html_nodes("tr/td[2]//a[@title]") %>% html_attr("href") })
Merci! Désolé, je pensais que les commentaires seraient assez bons, j'essaierai de mettre plus d'infos à l'avenir! –