使用rvest包抓取pm2.5静态页面数据,使用leafletCN包实现pm2.5数据的地图展示,代码如下所示:
library(rvest) library(leafletCN) Sys.setlocale("LC_CTYPE","eng") doc = read_html("http://www.pm25s.com/cn/rank/") cities = doc %>% html_nodes(".cityrank a") %>% html_text() AQI = doc %>% html_nodes("span[class^='lv']") %>% html_text() %>% .[c(F,F,T)] %>% as.numeric dat = data.frame(city=cities,AQI=AQI) dat geojsonMap(dat,"city", popup = paste0(dat$city,":",dat$AQI), palette = "Reds",legendTitle = "AQI")
执行结果如下