<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中提取“这是需要的文本”。我已经可以得到div类的“product_box_desc”,从中我可以得到它下面的文本“some text here”。但是我无法获取包含文本的跨度。这是我正在使用的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/

10-13 00:34