对于这个网站,您可以构建一个 url 并提交一个GET
请求以简单地获取 csv:
library(httr)
baseURL <- "http://uk-air.defra.gov.uk/data/laqm-background-maps.php"
queryList <- parse_url(baseURL)
queryList$query <- list("bkgrd-la" = 359, "bkgrd-pollutant" = "no2", "bkgrd-year" = 2011,
action = "data", year = 2011, submit = "Download+CSV")
res <- GET(build_url(queryList), write_disk("temp.csv"))
您可以通过解析原始页面来获取表单的代码:
library(XML)
doc <- htmlParse("http://uk-air.defra.gov.uk/data/laqm-background-maps?year=2011")
councils <- doc["//*[@id='bkgrd-la']/option", fun = function(x){
data.frame(value = xmlGetAttr(x, "value"), council = xmlValue(x))
}]
councils <- do.call(rbind.data.frame, councils)
> head(councils)
value council
1 359 Aberdeen City Council
2 360 Aberdeenshire Council
3 1 Adur District Council
4 2 Allerdale Borough Council
5 4 Amber Valley Borough Council
6 401 Anglesey County Council
pollutants <- doc["//*[@id='bkgrd-pollutant']/option", fun = function(x){
data.frame(value = xmlGetAttr(x, "value"), council = xmlValue(x))
}]
pollutants <- do.call(rbind.data.frame, pollutants)
> head(pollutants)
value council
1 no2 NO2
2 nox NOx
3 pm10 PM10
4 pm25 PM2.5
5 no2 NO2
6 nox NOx
ETC...