2012-11-20 3 views
5

का उपयोग करके पार्स आरएसएस फ़ीड निम्नलिखित आरएसएस फ़ीड http://www.huffingtonpost.com/rss/liveblog/liveblog-1213.xml को स्क्रैप और पार्स करने की कोशिश कर रहा हूं मैंने आर और एक्सएमएल के संबंध में अन्य प्रश्नों को देखा है और मेरी समस्या पर कोई प्रगति करने में असमर्थ रहे हैं। प्रत्येक प्रविष्टिएक्सएमएल पैकेजिन आर

 <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), "शीर्षक" (शीर्षक), "विवरण" (पूरा पाठ साफ) के लिए एक्सएमएल कोड। मैंने आर में एक्सएमएल पैकेज का उपयोग करने की कोशिश की है, लेकिन कबूल करता हूं कि मैं एक नौसिखिया हूं (एक्सएमएल के साथ काम करने के लिए कोई अनुभव नहीं, लेकिन कुछ आर अनुभव)। कोड मैं के बंद काम कर रहा हूँ, और साथ कहीं नहीं हो रही है:

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" 

बहरहाल, जब भी मैं में हेरफेर और "शीर्षक", या "विवरण" जानकारी में हेरफेर करने की कोशिश की कोशिश, मैं लगातार त्रुटियों मिलता है। इस कोड की समस्या निवारण में कोई भी मदद, सबसे सराहना की जाएगी।

धन्यवाद, थॉमस

उत्तर

10

मैं उत्कृष्ट Rcurl पुस्तकालय और xpathSApply उपयोग कर रहा हूँ

यह स्क्रिप्ट में अधिक जानकारी के xpathSApply के लिए आप 3 सूचियों (शीर्षक, pubdates और विवरण)

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

देता है एक्सएमएल पुस्तकालय में –