2015-02-05 4 views
0

Я хочу извлечь информацию из приведенного ниже HTML-кода, используя их классы или xpathSapply.Скребок HTML с использованием R - xpathSApply

Я хочу, чтобы захватить другую информацию в виде таблицы, например

  • эффективности в качестве колонки, заполненной 5 и полного комментарий

вместо

  • усеченных один, как столбец, заполненный полным комментарием.

<div class="userPost"> 
<div class="postHeading clearfix"> 
    <div class="conditionInfo"> 
       Condition: Condition in which Stomach Acid is Pushed Into the Esophagus</div> 
    <div class="date">8/12/2014 12:27:53 PM</div> 
</div> 
<p class="reviewerInfo">Reviewer: Believer, 35-44 Female on Treatment for 2 to less than 5 years (Patient) </p> 
<div id="ctnStars"> 
    <div class="catRatings firstEl clearfix"> 
    <p class="category">Effectiveness</p> 
    <p class="inlineRating starRating"><span class="current-rating" style="width: 100%"> 
     Current Rating: 5</span></p> 
    </div> 
    <div class="catRatings clearfix"> 
    <p class="category">Ease of Use</p> 
    <p class="inlineRating starRating"><span class="current-rating" style="width: 100%"> 
     Current Rating: 5</span></p> 
    </div> 
    <div class="catRatings lastEl clearfix"> 
    <p class="category">Satisfaction</p> 
    <p class="inlineRating starRating"><span class="current-rating" style="width: 100%"> 
     Current Rating: 5</span></p> 
    </div> 
</div> 
<p id="comTrunc1" class="comment"><strong>Comment: </strong><br>Most excellent! I tried several different rx&#39;s to help with my acid problem and none were as effective as Nexium. After being on it for 3 months I stopped because that was how long my doc thought it would take to heal me. I stopped taking it and boom, the pain was back. Got back on Nexium and am staying on it. Such relief was unexpected.</p> 
<p id="comFull1" class="comment" style="display:none"><strong>Comment:</strong><br>Most excellent! I tried several different rx&#39;s to help with my acid problem and none were as effective as Nexium. After being on it for 3 months I stopped because that was how long my doc thought it would take to heal me. I stopped taking it and boom, the pain was back. Got back on Nexium and am staying on it. Such relief was unexpected.<br><a onclick="toggle('comTrunc1'); toggle('comFull1');return false;" href="#">Hide Full Comment</a></p> 
<div class="actionLinks clearfix"> 
    <p class="helpful">4 
         people 

       found this review helpful.<br> 
       Was this review helpful? <span id="513102_Vote"><a href="#" onclick="return FoundHelpFul('8cbc5bf1-4f86-48e4-ac0f-5b3085949a2a', 513102, true)">Yes</a> | <a href="#" onclick="return FoundHelpFul('8cbc5bf1-4f86-48e4-ac0f-5b3085949a2a', 513102, false)">No</a></span></p><a class="reportAbuse" href="#" onclick="showPopWin('ReportAbuse.aspx?reviewid=513102&amp;userid=8cbc5bf1-4f86-48e4-ac0f-5b3085949a2a',400,160,null, false); return false;">Report This Post</a></div> 
+0

Привет и добро пожаловать в переполнение стека. Итак: какой код вы пробовали? это сломалось? если это так - каково сообщение об ошибке? В S/O мы предпочитаем помогать вам с тем, с чем вам приходилось, а не писать для вас, поэтому покажите нам, что у вас есть :) –

ответ

0

Это не для меня ясно, что вы делаете, но вот начало. Если это не так, как вы хотите, отредактируйте свой вопрос после того, как вы попробуете что-то в этом направлении (и включите свой код). Предполагая, что «url» - это URL-адрес веб-сайта, с которого вы предоставили код HTML, попробуйте что-то вроде этого:

library(xml) 
doc <- htmlTreeParse(url) # reads into the object doc the contents of the url 

data <- xpathSApply(doc, "//div[@id = 'ctnStars']//[[@class = 'category']", xmlValue, trim = TRUE) # to extract the value of that node ("Effectiveness")