php - 无法使用 xpath 获取 span 标记

标签 php html dom xpath

<div class="product_box">
    <div class="list_sale">
        <img src="link" class="listsale" alt="">
            <div class="product_box_title">
                <a href="link"><strong>title here</a></strong>
            </div>
            <div class="product_box_desc">
                some text here
                <strike>some text</strike> 
                <br />
                <span class="list_price">THIS IS THE NEEDED TEXT</span>
                <a href="link"><strong>some text</strong></a>
            </div>
            <div class="list_buynow">
            <form action="link" class="add_to_cart" method="post">
                <div class="add_cart">
                    <input type="image" src="link" value="add_to_cart" class="add_button">
                    <input id="fast_order_0_item_code" type="hidden" name="fast_order[0] [item_code]" value="value" class="item_code"/>
                    <input name="fast_order[0][add]" value="1" class="add_qty">
                    <input type="hidden" name="redirect_uri" value="value">
                </div>
            </form>
        </div>
       <div class="product_box_img">
           <a href="link">
               <a href="link"><img src="http://stacktoheap.com/images/stackoverflow.png" alt=""></a>
           </a>  
       </div>
   </div>
</div>

这是我的 html 文件,我需要从这个 div 中提取“THIS IS THE NEEDED TEXT”。我已经能够获得类为“product_box_desc”的 div,并且从中我能够获得它下面的文本“这里有一些文本”。但我无法获取包含文本的范围。这是我正在使用的 XPATH 查询,请提出必须更改的内容。

$dom_xpath->query("//div[@class='product_box']/div/div[@class='product_box_desc']/span[@class='list_price']")

最佳答案

这个查询对我来说很好用:

//div[@class="product_box"]/div[@class="list_sale"]/div[@class="product_box_desc"]/span[@class="list_price"]

但我将 html 更改为这个:

<div class='product_box'>
    <div class='list_sale'>
        <img src='link' class='listsale' alt='' />
        <div class='product_box_title'>
            <a href='link'><strong>title here</strong></a>
        </div>
        <div class='product_box_desc'>
            some text here
            <strike>some text</strike> 
            <br />
            <span class='list_price'>THIS IS THE NEEDED TEXT</span>
            <a href='link'><strong>some text</strong></a>
        </div>
        <div class='list_buynow'>
        <form action='link' class='add_to_cart' method='post'>
            <div class='add_cart'>
                <input type='image' src='link' value='add_to_cart' class='add_button'>
                <input id='fast_order_0_item_code' type='hidden' name='fast_order[0] [item_code]' value='value' class='item_code'/>
                <input name='fast_order[0][add]' value='1' class='add_qty'>
                <input type='hidden' name='redirect_uri' value='value'>
            </div>
        </form>
        </div>
       <div class='product_box_img'>
           <a href='link'>
               <img src='http://stacktoheap.com/images/stackoverflow.png' alt=''>
           </a>  
       </div>
   </div>
</div>

这里有一个错误:
<a href='link'><strong>title here</strong></a>而不是
<a href='link'><strong>title here</a></strong>

我这样做:

$nodes = ($xPath->query('//div[@class="product_box"]/div[@class="list_sale"]/div[@class="product_box_desc"]/span[@class="list_price"]'));

foreach($nodes as $node) {
    echo $node->textContent;
}

关于php - 无法使用 xpath 获取 span 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15544948/

相关文章:

javascript - 在 Yii 中的 Controller 的 <head> 中显示自定义 HTML

javascript - 更改 <span> 内的单选按钮文本

javascript - getElementsByTagName ("div").length 对任何网页返回零

javascript - 如何在保留点击处理程序的同时重新排列一组 html 元素?查询

php - Mysql中如何解析JSON字符串?

javascript - AJAX 上传仅显示多个文件上传中最后一个元素的进度

php - 从 MySQL->PostGres : Best Way to Store PHP Objects in MySQL, 在 PostGres 中检索?

JQuery Fancybox 不工作

html - 媒体查询不起作用?

javascript - 如何使用JavaScript为选项增加值(value)