2016-12-29 1 views
2

どのようにすればこのXMLを解析して、希望の結果を得ることができますか?私が試したすべての設定は、単一のベクトルで5つのa href=のリンクをまとめてグループ化していますが、2つの結果を<div class="entry-content">で区別する必要があります。ありがとう!xmlをrvestで子どもの数を変えて解析する方法

# xml snippet from 
# http://www.electionstudies.org/studypages/download/datacenter_all_NoData.html 
my_xml <- 
    '<li class="clearfix"> 
    <article class="entry-item"> 
    <div class="entry-content"> 
    <h4 class="entry-title"><img src="../../images/icons/timeseries.png"><a href="../anes_timeseries_cdf/anes_timeseries_cdf.htm">ANES Time Series Cumulative Data File</a> (1948-2012)</h4> 
    <p class="indented_text">Data documentation: &nbsp; <a href="../anes_timeseries_cdf/anes_timeseries_cdf.htm"> Study Page</a> &nbsp; <img src="../../images/icons/circle.png" /> &nbsp; <a href="../anes_timeseries_cdf/anes_timeseries_cdf_errata.htm">Errata</a></p> 
    </div><!--entry-content--> 
    </article><!--entry-item--> 
    </li> 
    <li class="clearfix"> 
    <article class="entry-item"> 
    <div class="entry-content"> 
    <h4 class="entry-title"><img src="../../images/icons/pilot.png"><a href="../anes_pilot_2016/anes_pilot_2016.htm">ANES 2016 Pilot Study</a></h4> 
    <p class="indented_text">Data documentation: &nbsp; <a href="../anes_pilot_2016/anes_pilot_2016.htm">Study Page</a></p> 
    </div><!--entry-content--> 
    </article><!--entry-item--> 
    </li>' 

# desired result 
list( 
    c("../anes_timeseries_cdf/anes_timeseries_cdf.htm" , "../anes_timeseries_cdf/anes_timeseries_cdf.htm" , "../anes_timeseries_cdf/anes_timeseries_cdf_errata.htm") , 
    c("../anes_pilot_2016/anes_pilot_2016.htm" , "../anes_pilot_2016/anes_pilot_2016.htm") 
) 

答えて

2
library(rvest) 
library(purrr) 

pg <- read_html("http://www.electionstudies.org/studypages/download/datacenter_all_NoData.html") 

html_nodes(pg, "article") %>% 
    map(~html_nodes(., "a") %>% 
     html_attr("href")) 

あなたは、最初のリストの結果を無視する必要があります。 CSSセレクタやXPathで結果を無視するソリューションが必要な場合は、私に知らせてください。

+1

これを可能にしてくれてありがとうございます。 "devesools :: install_github(" ajdamico/lodown ");ライブラリ(lodown); lodown(" anes "、output_dir =" C:/ My Directory/ANES "、your_email =" [email protected] ")' –

+0

*超軽量の袋。 – hrbrmstr

関連する問題