更改xpath代码以获取正确的信息php curl

时间:2013-09-02 08:33:27

标签: php xml curl xpath

好的,所以我有一个使用以下源代码的脚本

<h4>
<label id="ct0_l00_PageContainer_MyContainer_sym" for="symbol">
%
</label>59</h4>

my xpath code is 
$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$value = $xpath->query('//label[@id="ct0_l00_PageContainer_MyContainer_sym"]/following-sibling::text()')->item(0)->nodeValue;

这个查询工作正常,但在更改了几位之后就不再有效了,我有新的源代码,如下所示

<div id="ctl0_PageContainer_Container_divSummary">
                <div class="blueheaderbar">
                    <h2>
                        Summary</h2>
                </div>
                <div class="usesummary">
                    <div class="ccbody">
                        <ul>
                            <li class="greybox">
                                <div class="boxOutlineW">
                                    <span class="topLeft bg">
                                        <!--for IE6-->
                                    </span><span class="topRight bg">
                                        <!--for IE6-->
                                    </span><span class="bottomLeft bg">
                                        <!--for IE6-->
                                    </span><span class="bottomRight bg">
                                        <!--for IE6-->
                                    </span>
                                    <div class="content">
                                        <div class="pointbox">
                                            <h3>
                                                last 2 years</h3>
                                            <div class="boxOutlineG infoBoxwhite">
                                                <span class="topLeft bg">
                                                    <!--for IE6-->
                                                </span><span class="topRight bg">
                                                    <!--for IE6-->
                                                </span><span class="bottomLeft bg">
                                                    <!--for IE6-->
                                                </span><span class="bottomRight bg">
                                                    <!--for IE6-->
                                                </span>
                                                <div class="content">
                                                    <h4>
                                                        98%</h4>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </li>
                            <li class="greybox">
                                <div class="boxOutlineW">
                                    <span class="topLeft bg">
                                        <!--for IE6-->
                                    </span><span class="topRight bg">
                                        <!--for IE6-->
                                    </span><span class="bottomLeft bg">
                                        <!--for IE6-->
                                    </span><span class="bottomRight bg">
                                        <!--for IE6-->
                                    </span>
                                    <div class="content">
                                        <div class="pointsbox">
                                            <h3>
                                                You have used</h3>
                                            <div class="boxOutlineG infoBoxwhite">
                                                <span class="topLeft bg">
                                                    <!--for IE6-->
                                                </span><span class="topRight bg">
                                                    <!--for IE6-->
                                                </span><span class="bottomLeft bg">
                                                    <!--for IE6-->
                                                </span><span class="bottomRight bg">
                                                    <!--for IE6-->
                                                </span>
                                                <div class="content">
                                                    <h4>
                                                        91%</h4>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </li>
                            <li class="greybox">
                                <div class="boxOutlineW">
                                    <span class="topLeft bg">
                                        <!--for IE6-->
                                    </span><span class="topRight bg">
                                        <!--for IE6-->
                                    </span><span class="bottomLeft bg">
                                        <!--for IE6-->
                                    </span><span class="bottomRight bg">
                                        <!--for IE6-->
                                    </span>
                                    <div class="content">
                                        <div class="pointsbox">
                                            <h3>
                                                You still have </h3>
                                            <div class="boxOutlineG infoBoxwhite">
                                                <span class="topLeft bg">
                                                    <!--for IE6-->
                                                </span><span class="topRight bg">
                                                    <!--for IE6-->
                                                </span><span class="bottomLeft bg">
                                                    <!--for IE6-->
                                                </span><span class="bottomRight bg">
                                                    <!--for IE6-->
                                                </span>
                                                <div class="content">
                                                    <h4>
                                                        7%</h4>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>

如果有人可以帮助我,我需要为源的这部分写一个xpath

                                            You still have </h3>
                                            <div class="boxOutlineG infoBoxwhite">
                                                <span class="topLeft bg">
                                                    <!--for IE6-->
                                                </span><span class="topRight bg">
                                                    <!--for IE6-->
                                                </span><span class="bottomLeft bg">
                                                    <!--for IE6-->
                                                </span><span class="bottomRight bg">
                                                    <!--for IE6-->
                                                </span>
                                                <div class="content">
                                                    <h4>
                                                        7%</h4>

任何人都可以帮我改变这一点,以便获得7%的正确值

1 个答案:

答案 0 :(得分:0)

我建议你定位li哪些内容以“你还有”开头,然后选择h4元素

这转换为此表达式

.//li[@class="greybox"][starts-with(normalize-space(.//h3), "You still have")]//h4

您还可以使用

删除尾随的\n
normalize-space(
    .//li[@class="greybox"][starts-with(normalize-space(.//h3), "You still have")]//h4
)