celestialgod

rvest - goodinfo

Jan 9th, 2017
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.84 KB | None | 0 0
  1. library(installr)
  2. library(stringi)
  3. library(stringr)
  4. library(rvest)
  5. library(httr)
  6. library(pipeR)
  7. if (is.windows()) {
  8.   backupLocale <- Sys.getlocale("LC_COLLATE")
  9.   Sys.setlocale("LC_ALL", 'C')
  10. }
  11. htmlTbl <- GET("http://goodinfo.tw/StockInfo/StockFinDetail.asp",
  12.     query = list(RPT_CAT = "IS_M_QUAR_ACC", STOCK_ID = "2412")) %>>%
  13.   read_html %>>% html_node("table.solid_1_padding_3_4_tbl") %>>% html_table(fill = TRUE)
  14.  
  15. if (is.windows()) {
  16.   htmlTbl <- lapply(htmlTbl, function(v){
  17.     if (class(v) == "character") {
  18.       return(stri_conv(str_replace_all(v, "&nbsp|\\s", ""), to = "big5"))  # 字串都轉成big5
  19.     } else {
  20.       return(v)
  21.     }
  22.   }) %>>% `names<-`(NULL) %>>% as.data.frame %>>%  # names一定要先清空不然會錯
  23.     `names<-`(stri_conv(names(htmlTbl), to = "big5"))
  24.   Sys.setlocale(locale = backupLocale)
  25. }
Advertisement
Add Comment
Please, Sign In to add comment