Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html Nokogiri和Xpath:查找两个标记之间的所有文本_Html_Ruby_Xpath_Nokogiri - Fatal编程技术网

Html Nokogiri和Xpath:查找两个标记之间的所有文本

Html Nokogiri和Xpath:查找两个标记之间的所有文本,html,ruby,xpath,nokogiri,Html,Ruby,Xpath,Nokogiri,我不确定这是语法问题还是版本差异问题,但我似乎无法理解这一点。我想从h2标记到h3标记中获取(非关闭)td中的数据。下面是HTML的外观 <td valign="top" width="350"> <br><h2>NameIWant</h2><br> <br>Town<br> PhoneNumber<br> <a href="mailto:emailIwant@

我不确定这是语法问题还是版本差异问题,但我似乎无法理解这一点。我想从
h2
标记到
h3
标记中获取(非关闭)
td
中的数据。下面是HTML的外观

<td valign="top" width="350">
    <br><h2>NameIWant</h2><br>
    <br>Town<br>

    PhoneNumber<br>
    <a href="mailto:emailIwant@nowhere.com" class="links">emailIwant@nowhere.com</a>
    <br>
    <a href="http://websiteIwant.com" class="links">websiteIwant.com</a>
    <br><br>    
    <br><img src="images/spacer.gif"/><br>

    <h3><b>I want to stop before this!</b></h3>
    Lorem Ipsum Yadda Yadda<br>
    <img src="images/spacer.gif" border="0" width="20" height="11" alt=""/><br>
    <td width="25">
        <img src="images/spacer.gif" border="0" width="20" height="8" alt=""/>
        <td valign="top" width="200"><img src="images/spacer.gif"/>
            <br>
            <br>

            <table cellspacing="0" cellpadding="0" border="0"/>205"&gt;<tr><td>
                <a href="http://dontneedthis.com">
                </a></td></tr><br>
            <table border="0" cellpadding="3" cellspacing="0" width="200">
            ...

希望我能把我要做的事情说清楚。谢谢

查找单元格中第一个
前面的所有元素,然后检索所有前面没有
标记的同级元素作为前面的同级元素。用XPath表达式替换
//td
,以准确检索此表单元格

//td/h3[1]/preceding-sibling::*[preceding-sibling::h2]

这不是小事。在所选节点的上下文中(td),要获取两个元素之间的所有内容,需要执行这两个集合的交集:

  • 设置A:第一个
    h3
    /h3[1]/previous::node()前面的所有节点
  • 集合B:第一个
    h2
    /h2[1]/following::node()之后的所有节点
  • 要执行交叉,可以使用(之后,是谁提出的)。基本公式是:

    A[count(.|B) = count(B)]
    
    将其应用于您的集合,如上所述,其中A=
    //h3[1]/previous::node()
    ,B=
    //h2[1]/following::node()
    ,我们有:

    //h3[1]/preceding::node()[ count( . | //h2[1]/following::node()) = count(//h2[1]/following::node()) ]
    
    它将选择所有元素和文本节点,从
    标记后面的第一个

    开始,到最后一个

    标记后面的空白文本节点,就在下一个
    标记之前

    您可以轻松地仅选择表达式中
    h2
    h3
    之间的文本节点替换
    node()
    。这将返回两个标题之间的所有文本节点(包括空格和换行符):

    //h3[1]/preceding::text()[ count( . | //h2[1]/following::text()) = count(//h2[1]/following::text()) ]
    

    您还可以将这两个集合的选择器放入(Ruby)变量中,并对查询进行插值,这样可以使查询更加清晰。获取交集的另一种方法是进行两个单独的查询,并使用Ruby的
    &
    @matt查找两个结果节点集的交集,您可以添加该交集。这也是一个非常有趣的答案。这似乎很有效,非常感谢!也感谢Kaysian的参考;它为我清理了一些东西。我尝试使用这个,但它只是简单地返回“我正在使用@doc.xpath(“//h3[1]/前面的同级::*[preference sibling::h2]”)。到
    //h3[1]/preceding::node()[ count( . | //h2[1]/following::node()) = count(//h2[1]/following::node()) ]
    
    //h3[1]/preceding::text()[ count( . | //h2[1]/following::text()) = count(//h2[1]/following::text()) ]