Skip to content

Instantly share code, notes, and snippets.

@kirel
Created January 21, 2016 17:00
Show Gist options
  • Save kirel/bc7123fbe887eab0d85a to your computer and use it in GitHub Desktop.
Save kirel/bc7123fbe887eab0d85a to your computer and use it in GitHub Desktop.
library(rvest)
fetchPopWiki = function(query) {
tryCatch({
city <- read_html(paste0("https://www.wikipedia.de/go?q=",gsub(" ","+",query),"&l=de"))
table = city %>% html_nodes("table.infobox") %>%
html_table(fill=TRUE)
tds = city %>% html_nodes('th,td')
population = sub('([\\.0-9]+).*', '\\1', html_text(tds[grep('Einwohner|Bevölkerung', tds)[1] + 1])) # manchmal Bevölkerung
num = as.numeric(gsub('\\.','',population))
return(num)
}, error=function(e) return(NA))
}
fetchPopWiki = Vectorize(fetchPopWiki)
fetchPopWiki(c('Bad Saarow', 'Accra', 'Moskau', 'Doesnt exist'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment