celestialgod

Crawl Reservoir 2

Jun 12th, 2017
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.88 KB | None | 0 0
  1. library(data.table)
  2. library(stringi)
  3. library(lubridate)
  4. library(foreach)
  5. library(httr)
  6. library(xml2)
  7. library(rvest)
  8. library(pipeR)
  9.  
  10. # windows才需要先把locale改成C,並用stringi::stri_conv轉成big5
  11. backupLocale <- Sys.getlocale("LC_COLLATE")
  12. Sys.setlocale("LC_ALL", 'C')
  13.  
  14. timeVec <- seq(ymd_hms("2017/06/09 00:00:00"), ymd_hms("2017/06/11 00:00:00")-1, 600)
  15. reservoirUrl <- "http://fhy.wra.gov.tw/ReservoirPage_2011/Statistics.aspx" # 10 min data
  16.  
  17.  
  18. # for (time in timeVec) {
  19. time = timeVec[1]
  20.  
  21. getRes <- GET(reservoirUrl) %>>% content
  22.  
  23. POSTParas1 <- getRes %>>% {
  24.   list("__VIEWSTATE" = xml_find_all(., "//input[@name='__VIEWSTATE']"),
  25.        "__VIEWSTATEGENERATOR" = xml_find_all(., "//input[@name='__VIEWSTATEGENERATOR']"),
  26.        "__EVENTVALIDATION" = xml_find_all(., "//input[@name='__EVENTVALIDATION']")) %>>%
  27.     lapply(xml_attr, attr = "value")
  28. }
  29.  
  30. ctl00_ctl02_HiddenField <- getRes %>>% xml_find_all("//script") %>>% xml_attr("src") %>>%
  31.   `[`(grepl("AjaxControlToolkit", .)) %>>%
  32.   (sub("/ReservoirPage_2011/Statistics.aspx\\?_TSM_HiddenField_=", "", .))
  33.  
  34. POSTParas2 <- lapply(c(year, month, day, hour, minute), function(f) f(time)) %>>%
  35.   `names<-`(paste0("ctl00$cphMain$ucDate$cbo",
  36.                    c("Year", "Month", "Day", "Hour", "Minute")))
  37. postBody <- c(list("ctl00$cphMain$cboSearch" = "全部", "__EVENTTARGET" = "ctl00$cphMain$btnQuery",
  38.                    ctl00_ctl02_HiddenField = ctl00_ctl02_HiddenField,
  39.                    "ctl00$ctl02" = "ctl00$cphMain$ctl00|ctl00$cphMain$btnQuery"), POSTParas1, POSTParas2)
  40.  
  41. outTbl <- POST(reservoirUrl, body = postBody) %>>% content %>>%
  42.   xml_find_first("//table[@id='ctl00_cphMain_gvList']") %>>%
  43.   html_table(fill = TRUE) %>>% setDT %>>%
  44.   `[`(j = lapply(.SD, stri_conv, from = "UTF-8", to = "Big5"))
  45. setnames(outTbl, stri_conv(names(outTbl), "UTF-8", "Big5"))
  46.  
  47. # }
  48.  
  49. Sys.setlocale(locale = backupLocale)
Add Comment
Please, Sign In to add comment