fork download
  1. # Sys.setlocale(locale = "cht") # for windows OS
  2. library(rvest)
  3. library(magrittr)
  4.  
  5. # 以下我主要靠 xpath 去寫,但應該不難看懂。
  6. # 重點就是把頁面中第一個 div[@class='rt-tbody'] 之內的 div[@class='rt-td'] 都抓出來,
  7. # 再逐列排在 ncol = 6 的 matrix 之中就完成了。
  8. # 之後要不要轉 data.frame 或加上 colnames 或字串轉數字就先不說了,可以按需要自行操作。
  9. read_html("http://p...content-available-to-author-only...w.org/unit/%E8%A1%8C%E6%94%BF%E9%99%A2%E7%92%B0%E5%A2%83%E4%BF%9D%E8%AD%B7%E7%BD%B2") %>%
  10. html_nodes(xpath = "//div[@class='rt-tbody']") %>%
  11. .[[1]] %>%
  12. html_nodes(xpath = "div/div/div[@class='rt-td']") %>%
  13. html_text() %>%
  14. matrix(ncol = 6, byrow = T)
  15. # 結果為 100 * 6 的 matrix
Success #stdin #stdout #stderr 0.24s 39156KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error in library(rvest) : there is no package called ‘rvest’
Execution halted