【问题标题】:Bypass Style Formatting when Parsing RSS Feed in R在 R 中解析 RSS 提要时绕过样式格式
【发布时间】: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&amp;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


    【解决方案1】:

    你可以这样做:

    descriptions <- sapply(descriptions, function(x) {
      xmlValue(xmlRoot(htmlParse(x)))
    }, USE.NAMES=FALSE)
    

    给出(通过cat(stringr::str_wrap(descriptions[[1]], 70)):

    In a move that will provide young Europeans increased access to
    jobs and training opportunities, Nestlé and the Alliance for YOUth
    have joined the European Pact for Youth as founding members. Seven
    million people in Europe under the age of 25 are still inactive -
    neither in employment, education or training. The European Pact for
    Youth, created by European CSR business network CSR Europe and the
    European Commission, aims to work together with businesses, youth
    organisations, education providers and other stakeholders to reduce
    skills gaps and increase youth employability. As part of the Pact, the
    Alliance for YOUth will focus on setting up âdual learningâ schemes
    across Europe, combining formal education with apprenticeships and on-
    the-job training to help match skills with jobs on the market. The
    Alliance for YOUth is a group of almost 200 companies mobilised by
    Nestlé to help young people in Europe find work. It has pledged to
    create 100,000 employability opportunities by 2017 and has already met
    half of this target in its first year. Luis Cantarell, Executive Vice
    President for Nestlé and co-initiator of the European Pact for Youth,
    said: âPromoting a cultural shift to dual learning schemes based on
    business-education collaboration is at the heart of Nestléâs youth
    employment initiative since its start in 2013. The European Pact for
    Youth will help to build a skilled workforce and will tackle youth
    unemployment.â Learn more about the European Pact for Youth and read
    their press release.
    

    在生成的文本中(几乎在所有descriptions 中)的各个点都有\n 字符,但您可以将gsub 去掉。

    【讨论】:

    • 谢谢,这太好了。是否可以将cat 所示的输出作为文本分配给对象,从而正确转义 HTML?
    • 哦,是的,只需将str_wrap 调用的输出分配给一个变量。如果您不想使用另一个 pkg,base R 也有 strwrap
    • 这段代码非常适合我的目的。我刚刚注意到没有cat,某些特殊字符的表示是不同的。例如,str_wrap(descriptions[[1]], 70) 包括 â\u0080\u0098duallearningâ\u0080\u0099。这可能是与在没有cat 帮助的情况下转义 HTML 有关的编码问题吗?我可能会尝试gsub这些“剩菜”。
    • 嗯,这些是标准 ASCII 之上的字符,而不是 HTML 实体。你可以尝试iconv他们离开(即iconv(descriptions, to="Latin1"))。我还在sapply 转换中添加了USE.NAMES=FALSE,以删除不需要的属性。
    • 这完全符合要求。谢谢你这么清楚的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-20
    • 2012-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多