celestialgod

Taiwan weather data

May 2nd, 2016
747
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.39 KB | None | 0 0
  1. library(stringi)
  2. library(stringr)
  3. library(xml2)
  4. library(pipeR)
  5. library(purrr)
  6. dat <- read_html("http://www.cwb.gov.tw/V7/climate/monthlyData/Data/mD201512.htm", "UTF-8") %>>%
  7.   xml_find_all('//table[@class="Form00"]/tr') %>>%
  8.   map(~ when(.,
  9.       identical(class(try({xml_find_one(., 'td')})), "try-error") ~ xml_find_all(., 'th'),
  10.       ~ xml_find_all(., 'td')
  11.     ) %>>% xml_text %>>%
  12.     when(
  13.       identical(.Platform$OS.type, "windows") ~ stri_conv(., "UTF-8", "BIG5"),
  14.       ~ .
  15.     ) %>>%
  16.     str_replace_all("\\s", "")
  17.   )
  18.  
  19. # 最上面的表頭
  20. dat[[1]]
  21. # [1] "項目"                           "溫度(℃)"                   "雨量"
  22. # [4] "風速(公尺/秒)/風向(360°)/日期" "相對溼度(%)"                "測站氣壓"
  23. # [7] "降水日數>=0.1毫米"              "日照時數"
  24.  
  25. # 下面的表格
  26. do.call(rbind, dat[2:length(dat)])
  27. ## 印出部分
  28. #      [,1]     [,2]   [,3]        [,4]        [,5]     [,6]
  29. # [1,] "測站"   "平均" "最高/日期" "最低/日期" "(毫米)" "最大十分鐘風"
  30. # [2,] "阿里山" "9.8"  "18.0/5"    "2.8/29"    "56.8"   "4.8/340.0/26"
  31. # [3,] "鞍部"   "12.3" "23.7/22"   "2.5/17"    "245.7"  "14.3/350.0/10"
  32. # [4,] "板橋"   "18.8" "29.9/22"   "11.1/17"   "80.6"   "7.1/70.0/19"
  33. # [5,] "成功"   "21.1" "27.8/21"   "14.5/17"   "86.4"   "9.8/30.0/31"
  34. # [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