Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(stringi)
- library(stringr)
- library(xml2)
- library(pipeR)
- library(purrr)
- dat <- read_html("http://www.cwb.gov.tw/V7/climate/monthlyData/Data/mD201512.htm", "UTF-8") %>>%
- xml_find_all('//table[@class="Form00"]/tr') %>>%
- map(~ when(.,
- identical(class(try({xml_find_one(., 'td')})), "try-error") ~ xml_find_all(., 'th'),
- ~ xml_find_all(., 'td')
- ) %>>% xml_text %>>%
- when(
- identical(.Platform$OS.type, "windows") ~ stri_conv(., "UTF-8", "BIG5"),
- ~ .
- ) %>>%
- str_replace_all("\\s", "")
- )
- # 最上面的表頭
- dat[[1]]
- # [1] "項目" "溫度(℃)" "雨量"
- # [4] "風速(公尺/秒)/風向(360°)/日期" "相對溼度(%)" "測站氣壓"
- # [7] "降水日數>=0.1毫米" "日照時數"
- # 下面的表格
- do.call(rbind, dat[2:length(dat)])
- ## 印出部分
- # [,1] [,2] [,3] [,4] [,5] [,6]
- # [1,] "測站" "平均" "最高/日期" "最低/日期" "(毫米)" "最大十分鐘風"
- # [2,] "阿里山" "9.8" "18.0/5" "2.8/29" "56.8" "4.8/340.0/26"
- # [3,] "鞍部" "12.3" "23.7/22" "2.5/17" "245.7" "14.3/350.0/10"
- # [4,] "板橋" "18.8" "29.9/22" "11.1/17" "80.6" "7.1/70.0/19"
- # [5,] "成功" "21.1" "27.8/21" "14.5/17" "86.4" "9.8/30.0/31"
- # [6,] "嘉義" "19.8" "30.2/24" "10.4/19" "31.4" "8.4/20.0/10"
Advertisement
Add Comment
Please, Sign In to add comment