【发布时间】:2015-11-17 10:54:59
【问题描述】:
我正在尝试抓取并解析以下 RSS 提要 http://www.nestle.com/_handlers/rss.ashx?q=068f9d6282034061936dbe150c72d197。使用以下代码提取我需要的基本项目(例如,标题、描述、pubDate)没有问题:
library(RCurl)
library(XML)
xml.url <- "http://www.nestle.com/_handlers/rss.ashx?q=068f9d6282034061936dbe150c72d197"
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)
我的问题是项目“描述”的输出不仅包括实际文本,还包括许多样式格式表达式。例如,第一个元素是:
descriptions[1]
[1] "<p><iframe height=\"322\" src=\"https://www.youtube-nocookie.com/embed/fhESDXnlMa0?rel=0\" frameBorder=\"0\" width=\"572\"></iframe><br />\n<br />\n<p><em>Nescafé</em> is partnering with Facebook to launch an immersive video, pioneering new technology just released for the platform.</p>\n<p>\nThe <em>Nescafé</em> <a class=\"externalLink\" title=\"Opens in a new window: Nescafé on Facebook\" href=\"https://www.facebook.com/Nescafe/videos/vb.203900255471/10156233581755472/?type=2&theater\" target=\"_blank\">‘Good Morning World’ video</a> stars people in kitchens across the world, performing the hit song ‘Don’t Worry’ using spoons, cups, forks and a jar of coffee. Uniquely, viewers can rotate their smartphones through 360˚ to explore the video, the first time this has been possible on Facebook.</p>\n<p>\n“We know young coffee lovers pick up their phone at the start of every day looking to be entertained by real experiences. The 360˚ video allows us to be engaging in an innovative way,” said Carsten Fredholm, Senior Vice President of Nestlé’s Beverage Strategic Business Unit.\n</p>\n<p><em>Nescafé</em> recently teamed up with Google to offer the <a href=\"/media/news/pages/nescafe-google-virtual-reality-coffee-experience.aspx\">first virtual reality coffee experience</a> through the <em>Nescafé 360˚</em> app. It also became the first global brand to <a href=\"/media/news/pages/nescafe-moves-to-tumblr.aspx\">move its website onto Tumblr</a>, to strengthen connections with younger fans by allowing them to create and share content.</p>\n<p>The Nestlé brand is one of only six globally to partner Facebook for the launch of this technology.</p></p>"
我可以想到一种正则表达式方法来替换不需要的字符串。但是,有没有办法直接通过 xpath 访问项目“描述”的纯文本元素?
非常感谢您对此问题的任何帮助。谢谢。
【问题讨论】:
标签: r xpath xml-parsing rss