2012-11-20 21 views
5

Şu RSS beslemeyi kazıp ayırmaya çalışıyorum http://www.huffingtonpost.com/rss/liveblog/liveblog-1213.xml R ve XML ile ilgili diğer sorguları inceledim ve sorunumda herhangi bir ilerleme kaydedemedim. her giriş Ben "Tarih" kaydetmek istediğiniz her giriş/gönderimizdeXML paketini kullanarak RSS beslemeyi ayrıştırın R

 <item> 
    <title><![CDATA[Five Rockets Intercepted By Iron Drone Systems Over Be'er Sheva]]></title> 
    <link>http://www.huffingtonpost.co.uk/2012/11/15/tel-aviv-gaza-rocket_n_2138159.html#2_five-rockets-intercepted-by-iron-drone-systems-over-beer-sheva</link> 
    <description><![CDATA[<a href="http://www.haaretz.com/news/diplomacy-defense/live-blog-rockets-strike-tel-aviv-area-three-israelis-killed-in-attack-on-south-1.477960" target="_hplink">Haaretz reports</a> that five more rockets intercepted by Iron Dome systems over Be'er Sheva. In total, there have been 274 rockets fired and 105 intercepted. The IDF has attacked 250 targets in Gaza.]]></description> 
    <guid>http://www.huffingtonpost.co.uk/2012/11/15/tel-aviv-gaza-rocket_n_2138159.html#2_five-rockets-intercepted-by-iron-drone-systems-over-beer-sheva</guid> 
    <pubDate>2012-11-15T12:56:09-05:00</pubDate> 
    <source url="http://huffingtonpost.com/rss/liveblog/liveblog-1213.xml">Huffingtonpost.com</source> 
    </item> 

(pubDate), "Başlık" (başlık), "Açıklama" (temizlenmiş tam metin) için xml kodu. R'de xml paketini kullanmayı denedim, ama itiraf etmeliyim ki biraz yeni bir kullanıcıyım (XML ile çalışma deneyiminden çok azına, fakat bazı R deneyimlerine). Ben kapalı çalışma ve birlikte hiçbir yerde alıyorum kod şudur: Ancak

library(XML) 

xml.url <- "http://www.huffingtonpost.com/rss/liveblog/liveblog-1213.xml" 

# Use the xmlTreePares-function to parse xml file directly from the web 

xmlfile <- xmlTreeParse(xml.url) 

# Use the xmlRoot-function to access the top node 

xmltop = xmlRoot(xmlfile) 

xmlName(xmltop) 

names(xmltop[[ 1 ]]) 

    title   link description  language  copyright 
    "title"  "link" "description" "language" "copyright" 
category  generator   docs   item   item 
    "category" "generator"  "docs"  "item"  "item" 

, ben işlemek ve "title" veya "açıklama" bilgisini işlemek için denemek istediğiniz her seferinde, ben sürekli hatalar olsun. Bu kodun giderilmesiyle ilgili herhangi bir yardım, en çok takdir edilecektir.

sayesinde Thomas

Ben mükemmel Rcurl kütüphane ve xpathSApply kullanıyorum

cevap

10

Bu komut dosyası daha fazla bilgi xpathSApply için size 3 listeleri (başlık, pubdates ve açıklama)

library(RCurl) 
library(XML) 
xml.url <- "http://www.huffingtonpost.com/rss/liveblog/liveblog-1213.xml" 
script <- getURL(xml.url) 
doc  <- xmlParse(script) 
titles <- xpathSApply(doc,'//item/title',xmlValue) 
descriptions <- xpathSApply(doc,'//item/description',xmlValue) 
pubdates <- xpathSApply(doc,'//item/pubDate',xmlValue) 
+0

verir ise XML kitaplığında –

İlgili konular