Created
November 4, 2014 18:20
-
-
Save briatte/499b960f59cabfc6e921 to your computer and use it in GitHub Desktop.
exemple de scraper pour les scrutins publics de l'Assemblée nationale (attention, bug sur certaines listes avec un seul votant, car pas de tag <li> dans le code HTML)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(XML) | |
| library(stringr) | |
| # demo page | |
| h = htmlParse("http://www2.assemblee-nationale.fr/scrutins/detail/(legislature)/14/(num)/947") | |
| g = c("SRC", "UMP", "UDI", "ECOLO", "RAD", "GDR") | |
| k = c("socialiste", "mouvement populaire", "démocrates", "écologiste", "radical", "gauche") | |
| for(i in k) { | |
| cat("\nGROUPE :", g[ which(k == i) ], "\n") | |
| for(j in c("Pour", "Contre", "Abstention", "Non-votant")) { | |
| v = paste0("//div[@id='analyse']//p[@class='nomgroupe' and contains(., '", i) | |
| v = paste0(v, "')]/following-sibling::div[@class='", j, "']/ul/li") | |
| v = xpathSApply(h, v, xmlValue) | |
| if(length(v)) { | |
| cat("\n", j, ":", length(v), ":", paste0(str_trim(v), collapse = ", "), "\n") | |
| } else { | |
| cat("\n", j, ": 0 (apparemment)\n") | |
| } | |
| } | |
| } | |
| # kthxbye | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment