Re: [問題] table問題(網頁編碼)
※ 引述《celestialgod (天)》之銘言:
: 我不知道網頁的header跟內文的關係
: 我看charset是big5,不過我實際讀表格的資料,還是要用UTF8
: 所以我抓content的時候用big5是正常的,再用read_html with encoding utf8
: 之後再把utf8轉回去big5 (windows才要轉,mac, linux不用stri_conv那段)
: 之後就可以看到正常的表格內容了
: library(httr)
: library(pipeR)
: library(xml2)
: library(stringi)
: library(stringr)
: tableContent <- GET("http://depart.femh.org.tw/HMC/wholebody.html") %>>%
: content("text", encoding = "BIG5") %>>% read_html("UTF-8") %>>%
: xml_find_all("//tr/td/table/tbody/tr") %>>% lapply(function(x){
: output_text <- xml_find_all(x, "td") %>>% xml_text %>>%
: stri_conv(from = "UTF-8", to = "Big5") %>>% str_replace_all("\\s", "")
: if (length(output_text) >= 9 && length(output_text) <= 11)
: {
: return(c(rep("", 12-length(output_text)), output_text))
: } else if (length(output_text) == 8)
: {
: return(c(output_text[1:4], unlist(rbind(output_text[5:8], rep("", 4)))))
: } else
: {
: return(output_text)
: }
: }) %>>% do.call(what = rbind)
: 結果截圖:
: http://i.imgur.com/eDG4uEY.png

: 函數說明可以往前翻我的文章,某一篇(#1N9lFXFI (R_Language))下面有一些說明
: 不懂再回文發問吧
: #那串的用法:http://evolutionbrain.blogspot.tw/2015/08/ptt.html
因為我是新手剛學R,看不太懂 str_replace_all("\\s", "") 這段程式,
還有,
if (length(output_text) >= 9 && length(output_text) <= 11)
{
return(c(rep("", 12-length(output_text)), output_text))
} else if (length(output_text) == 8)
{
return(c(output_text[1:4], unlist(rbind(output_text[5:8], rep("", 4))))
} else
{
return(output_text)
}
}) %>>% do.call(what = rbind)
尤其是那些數字,不太懂他們代表什麼意思,如果逐一註解更好,不方便的話沒有關係
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 120.96.63.165
※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1465198177.A.A7C.html
→
06/06 16:27, , 1F
06/06 16:27, 1F
→
06/06 16:28, , 2F
06/06 16:28, 2F
→
06/06 16:28, , 3F
06/06 16:28, 3F
討論串 (同標題文章)
R_Language 近期熱門文章
PTT數位生活區 即時熱門文章