Created
September 10, 2020 14:58
-
-
Save francisbarton/9ca9d8566f5cfd86f5dd6657408a4986 to your computer and use it in GitHub Desktop.
Use the doogal.co.uk API to get data about a postcode
This file contains 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
# Use the doogal.co.uk API to get data about a postcode | |
# Doesn't accept a vector of codes all at once, so use with purrr::map_dfr() | |
# along a vector to combine results into a data frame | |
get_doogal_data <- function(postcode) { | |
data_names <- c( | |
"postcode", | |
"latitude", | |
"longitude", | |
"quality", | |
"constituency", | |
"district", | |
"ward", | |
"lsoa11nm", | |
"county", | |
"region", | |
"country", | |
"national_park", | |
"altitude", | |
"rural_urban" | |
) | |
endpoint <- "https://www.doogal.co.uk/GetPostcode.ashx?postcode=" | |
paste0(endpoint, postcode) %>% | |
utils::URLencode() %>% | |
xml2::read_html() %>% | |
rvest::html_node("p") %>% | |
rvest::html_text() %>% | |
readr::read_delim("\t", col_names = data_names) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment