2016-08-19 19 views
0

私はこの問題を解決しようとしてStackoverflowのユーザーからいくつかの助けを借りていました。しかし、私は新しい問題に走った:Webスクラッピングhtmlテーブルを使用してR

URL <- "http://karakterstatistik.stads.ku.dk/Histogram/ASOB05038E/Summer-2015" 
pg <- read_html(URL) 

get_val <- function(x, label) { 
xpath <- sprintf(".//table/tr/td[contains(., '%s')][1]/following-sibling::td", label) 
html_nodes(x, xpath=xpath) %>% 
html_text() %>% 
trimws() 
} 

library("stringr") 
trimmed = get_val(pg, "Karakter") %>% 
    str_replace_all(pattern = "\\n|\\t|\\r" , 
       replacement = "") 
trimmed 

私は再受験や試験の両方のための試験の結果を取得したい、しかし、2つのテーブルの見出しの両方が同じであるため、Rのみから値をとります再就職。 具体的には、見出しの下の両方の表の12,10,7,4,02、00、-3のグレードの隣に列 "アンタル"を表示したいと思います。

たくさんありがとう! :)

答えて

0
results <- html_nodes(pg, xpath=".//td[@style='width: 50%;' and 
             descendant::h3[contains(text(), 'Resultater')]]/table") 

html_table(results[[1]]) 
## Karakter Antal Antal 
## 1  12 11 (9,6 %) 
## 2  10 48 (41,7 %) 
## 3  7 41 (35,7 %) 
## 4  4  4 (3,5 %) 
## 5  02  1 (0,9 %) 
## 6  00  1 (0,9 %) 
## 7  -3  4 (3,5 %) 
## 8 Ej mødt  5 (4,3 %) 

html_table(results[[2]]) 
## Karakter Antal Antal 
## 1  12  0 (0,0 %) 
## 2  10  0 (0,0 %) 
## 3  7  1 (9,1 %) 
## 4  4  1 (9,1 %) 
## 5  02  1 (9,1 %) 
## 6  00  1 (9,1 %) 
## 7  -3  0 (0,0 %) 
## 8 Ej mødt  7 (63,6 %) 
関連する問題