【问题标题】:xpath: extract only class that start with [duplicate]xpath:仅提取以 [重复] 开头的类
【发布时间】:2018-10-27 05:28:26
【问题描述】:

我有一个看起来像这样的 html:

<div class="date_s">May 16, 2018</div>
<div class="date_a">May 17, 2018</div>
<div class="date_g23">May 18, 2018</div>

我可以提取一个特定的 div 类:

XML::xpathSApply(XML::htmlParse(myurl), "//div[@class='date_s']", XML::saveXML)

如何提取所有以 'date' 开头的 div 类?

我试过了(返回一个空列表):

XML::xpathSApply(XML::htmlParse(myurl), "//div[starts-with(name(), 'date')]", XML::saveXML)

【问题讨论】:

    标签: html r xml xpath


    【解决方案1】:

    你的代码基本正确,只是用@class代替name()

    XML::xpathSApply(XML::htmlParse(myurl), 
                     "//div[starts-with(@class, 'date')]", XML::saveXML)
    
    [1] "<div class=\"date_s\">May 16, 2018</div>"  
    [2] "<div class=\"date_a\">May 17, 2018</div>"  
    [3] "<div class=\"date_g23\">May 18, 2018</div>"
    

    【讨论】:

      【解决方案2】:

      您可以通过

      获得整个列表
      document.querySelectorAll("div[class^='date']" );
      

      【讨论】:

        猜你喜欢
        • 2013-06-26
        • 1970-01-01
        • 2013-01-29
        • 2018-10-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-10
        相关资源
        最近更新 更多