2017-10-22 5 views
1

rbit 패키지를 사용하여 input type = "radio"태그와 TAG span class = "glyphicon glyphicon-ok"에 저장된 텍스트를 읽을 수 있습니까? 예 : 나는rvest를 사용하여 HTML에서 읽기

R 코드 #does이 p_ans에 저장됩니다

install.packages('rvest') 
library('rvest') 

url <- 'http://upscfever.com/upsc-fever/en/test/en-test-sci1.html' 

webpage <- read_html(url) 

p_ans <- webpage %>% 
     html_nodes("input + glyphicon-ok") %>% 
     html_text() 

HTML 코드

<div class="form-group" id="myform"> 
      <label for="usr">Q1: Energy giving foods are </label> 
    </div> 
    <div class="radio"> 
     <label><input type="radio" value="1" name="optradio0">Carbohydrates and fats<span class="glyphicon glyphicon-ok"></span></label> 
    </div> 
    <div class="radio"> 
     <label><input type="radio" id="opt1" value="-0.33" name="optradio0">Carbohydrates and Proteins<span id="sp1" class="glyphicon glyphicon-remove"></span></label> 
    </div> 
+0

작업 및 NA를주지 문자 벡터에 "탄수화물과 지방을"읽고 싶어 특정 퀴즈에 대한 모든 정답을 찾아내는 아주 영리한 방법 ;-) – hrbrmstr

답변

0
library(rvest) 

pg <- read_html("http://upscfever.com/upsc-fever/en/test/en-test-sci1.html") 
html_nodes(pg, xpath=".//label[input and span[contains(@class, 'glyphicon glyphicon-ok')]]") %>% 
    html_text() 
## [1] "Carbohydrates and fats"           
## [2] "saturated fatty acids"           
## [3] "unsaturated fatty acids are good for health"      
## [4] "unsaturated fats"            
## [5] "polypeptides"             
## [6] "Maerasmus"              
## [7] "Ribulose bisphosphate Carboxylase-Oxygenase "     
## [8] "Mercury"               
## [9] "Cadmium"               
## [10] "Absorb free radicals"           
## [11] "A"                
## [12] "Calcium - Goitre"            
## [13] "none"               
## [14] "Excretion of undigested food"         
## [15] " complex components of food are broken into simpler substances." 
## [16] "starch to sugar"             
## [17] "protection of stomach lining"         
## [18] "Liver"               
## [19] "digestion of fats"            
## [20] "only HDC is good"            
## [21] "35-42"               
## [22] "absorption of food"            
## [23] "digest cellulose"            
## [24] "meat is easily digested"           
## [25] "gall bladder"     
+0

결코 발생하지 않는 생각. – UPSCFever