Re: [問題] 如何將html_nodes的資料轉成CSV
※ 引述《lovedmagic (EricZou)》之銘言:
: [問題類型]:
: 程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來)
: [軟體熟悉度]:
: 新手(沒寫過程式,R 是我的第一次)
: [問題敘述]:
: 參考了一下板上的資訊,對於用rvest轉CSV檔還是不瞭解
: 請求指點迷津
: [程式範例]:
: if (!"installr" %in% installed.packages()) install.packages("installr")
: library(installr)
: require2(rvest)
: require2(stringr)
: require2(pipeR)
: ateam1 <- read_html("https://www.kickstarter.com/mexico?ref=hero")
: asd = html_nodes(ateam1, "ul.project-stats")
: html_text(asd)
: [1] "\n53% funded\n\n\nMX$ 24,235 pledged\n\n\nFunded\n\n\n\n\n\n
: [2] "\n4% funded\n\n\nMX$ 7,305 pledged\n\n\nFunded\n\n\n\n\n\n
: [3] "\n67% funded\n\n\nMX$ 54,050 pledged\n\n\nFunded\n\n\n\n\n\n
: 如果我將資料匯出成CSV有什麼辦法呢?直接用write.csv會出現他是nodes
: 所以不能用data.frame的方式輸出,而其實我只要53%- $24235 、4%
: - $7305 這兩個資料,那我又有什麼其他的方法可以完成呢?
: [環境敘述]:
: Windows 7
: R 3.2.2
: [關鍵字]:
: html、網路爬蟲
使用html_structure去看html的架構再根據架構去剖析出你要的資訊即可
示範如下:
if (!"installr" %in% installed.packages()) install.packages("installr")
library(installr)
require2(rvest)
require2(pipeR)
require2(plyr)
stats <- read_html("https://www.kickstarter.com/mexico?ref=hero") %>>%
html_nodes("ul.project-stats")
# html_structure(stats[[1]]) # 用來查看node資料資訊
laply(stats, function(x){
c(
funded = html_node(x, "div.project-stats-value") %>>% html_text,
pledged = html_node(x, "span.money") %>>% html_text
)
})
可能剩下就是處理一些文字的,例如把%數跟$轉成數字
--
R資料整理套件系列文:
magrittr #1LhSWhpH (R_Language) https://goo.gl/72l1m9
data.table #1LhW7Tvj (R_Language) https://goo.gl/PZa6Ue
dplyr(上.下) #1LhpJCfB,#1Lhw8b-s (R_Language) https://goo.gl/I5xX9b
tidyr #1Liqls1R (R_Language) https://goo.gl/i7yzAz
pipeR #1NXESRm5 (R_Language) https://goo.gl/zRUISx
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.232.188.79
※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1480856978.A.EBB.html
推
12/04 21:23, , 1F
12/04 21:23, 1F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):
R_Language 近期熱門文章
PTT數位生活區 即時熱門文章