【问题标题】:Extracting content from within meta itemprop using R使用 R 从 meta itemprop 中提取内容
【发布时间】:2015-03-03 18:44:50
【问题描述】:

我正在尝试从以下内容部分中提取文本。

<meta itemprop="datePublished" content="2015-02-24T11:34:57Z"/>
<meta itemprop="description" content="South Africa's gross domestic output 
(GDP) expanded by a much stronger 4.1 percent in the fourth quarter"/>

我对从“datePublished”和“description”中提取时间和日期特别感兴趣。现在有趣的是我可以提取“描述”文本,使用

xpathSApply(story2, "//head/meta[@name=\"description\"]/@content")

但不是datePublished,即使我使用的语法相同,只产生NULL 结果。

我无法解释这一点,想知道是否有人知道为什么xpathSApply 在提取描述时不提取datePublished,以及正确的格式是什么。我也无法通过 grep 使用子字符串操作来提取它。

【问题讨论】:

    标签: regex xml r xpath sapply


    【解决方案1】:

    这是您正在尝试的代码吗,因为我认为它应该是:

    '<meta itemprop="datePublished" content="2015-02-24T11:34:57Z"/>
    <meta itemprop="description" content="South Africa\'s gross domestic output (GDP) expanded by a much stronger 4.1 percent in the fourth quarter"/>' -> foo
    library(XML)
    bar <- xmlParse(foo)
    datePublished <- xpathSApply(bar, "//meta[@itemprop='datePublished']/@content")
    description <- xpathSApply(bar, "//meta[@itemprop='description']/@content")
    

    【讨论】:

      猜你喜欢
      • 2014-06-14
      • 1970-01-01
      • 1970-01-01
      • 2015-07-07
      • 2019-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多