HtmlAgilityPack - 使用 XPath 从节点集合中选择单个节点

发布于 2024-11-26 00:21:41 字数 3797 浏览 3 评论 0原文

我已经与这段代码斗争了几个小时了......

结果示例节点:

<div class="left vcard" sizcache="1" sizset="32">
        <h2 class="clearfix fn org url" sizcache="1" sizset="32">
            <a id="listItemTitle_11310540" href="/marcali/viragok-viragkuldes/11310540/ANDOK_MATYAS/hirdetes.mtt">ANDÓK MÁTYÁS</a> <a class="removeFromList" href="#">törlés</a>
        </h2>
        <p class="description">
            2009 decemberében nyitottuk meg az Ezerszirom Virágbolt-ot Marcaliban a Petőfi Sándor u.11-ben. Szeretettel várja a kedves vásárlókat Horváth Györgyike virágkötő, aki 15 éve kápráztatja el kreatíva...</p>
        <ul class="profession" sizcache="1" sizset="34">
            <li sizcache="1" sizset="34"><a href="/szakmak/viragok-viragkuldes/index.mtt">Virágok, virágküldés</a> </li>
        </ul>
        <div class="clearfix margined" sizcache="1" sizset="35">
            <p class="address adr">
                <span>Cím:</span> 8700 Marcali, Petőfi S. utca 11 .</p>
            <ul class="nav clearfix" sizcache="1" sizset="35">
                <li class="mapLink" sizcache="1" sizset="35"><span>-</span><a class="mapLink" href="/terkep/11310540/ANDOK_MATYAS">térképen mutat</a> </li>
                <li class="routeplanner" sizcache="1" sizset="36"><em>útvonaltervezés</em> <span>-</span> <a onclick="window.location = '/redirect.jspv?method=Redirect&amp;routePlanCount=11310540&amp;url=%2Futvonalterv.jspv%3Fto%3D%26from%3D8700+Marcali%2C+Pet%C5%91fi+S.+utca+11+.%26fromX%3D17.414611005499122%26fromY%3D46.57928886409497%26activeTab%3DrouteSearch&amp;activeTab=routeSearch'" href="#">innen</a> <strong>/</strong> <span>-</span> <a onclick="window.location = '/redirect.jspv?method=Redirect&amp;routePlanCount=11310540&amp;url=%2Futvonalterv.jspv%3Ffrom%3D%26to%3D8700+Marcali%2C+Pet%C5%91fi+S.+utca+11+.%26toX%3D17.414611005499122%26toY%3D46.57928886409497%26activeTab%3DrouteSearch'" href="#">ide</a> </li>
            </ul>
            <div class="contacts" sizcache="1" sizset="38">
                <div class="phoneHolder clearfix" sizcache="1" sizset="38">
                    <div class="dt phone">
                        Telefonszám:
                    </div>
                    <div class="dd phoneValue tel" sizcache="1" sizset="38">
                        <span>(70) 326 2843</span> <a class="clickToCall" onclick="javascript:openECS('11310540');" href="#">Hívja ingyen!</a>
                    </div>
                </div>
                <div class="webLinkHolder" sizcache="1" sizset="39">
                    <div class="dt webLink clearfix">
                        Weboldal:
                    </div>
                    <div class="dd webLinkValue" sizcache="1" sizset="39">
                        <a href="http://www.ezerszirom.hu" rel="blank,nofollow">http://www.ezerszirom.hu</a>
                    </div>
                </div>
            </div>
        </div>
    </div>

我的代码:

            results = htmlDoc.DocumentNode.SelectNodes("//div[contains(@class, 'vcard')]");
            if (results != null)
            {
                foreach (var node in results)
                {
                    Response.Write(node.SelectSingleNode("//p[@class='description']").InnerText + "<br>");
                }
            }

问题是什么? 由于我正在迭代结果,因此每个节点都应该有自己的描述, 但我正在获取第一个节点的 InnerText。 知道为什么吗?

i have been fighting with this code for a few hours now....

Sample Node for results:

<div class="left vcard" sizcache="1" sizset="32">
        <h2 class="clearfix fn org url" sizcache="1" sizset="32">
            <a id="listItemTitle_11310540" href="/marcali/viragok-viragkuldes/11310540/ANDOK_MATYAS/hirdetes.mtt">ANDÓK MÁTYÁS</a> <a class="removeFromList" href="#">törlés</a>
        </h2>
        <p class="description">
            2009 decemberében nyitottuk meg az Ezerszirom Virágbolt-ot Marcaliban a Petőfi Sándor u.11-ben. Szeretettel várja a kedves vásárlókat Horváth Györgyike virágkötő, aki 15 éve kápráztatja el kreatíva...</p>
        <ul class="profession" sizcache="1" sizset="34">
            <li sizcache="1" sizset="34"><a href="/szakmak/viragok-viragkuldes/index.mtt">Virágok, virágküldés</a> </li>
        </ul>
        <div class="clearfix margined" sizcache="1" sizset="35">
            <p class="address adr">
                <span>Cím:</span> 8700 Marcali, Petőfi S. utca 11 .</p>
            <ul class="nav clearfix" sizcache="1" sizset="35">
                <li class="mapLink" sizcache="1" sizset="35"><span>-</span><a class="mapLink" href="/terkep/11310540/ANDOK_MATYAS">térképen mutat</a> </li>
                <li class="routeplanner" sizcache="1" sizset="36"><em>útvonaltervezés</em> <span>-</span> <a onclick="window.location = '/redirect.jspv?method=Redirect&routePlanCount=11310540&url=%2Futvonalterv.jspv%3Fto%3D%26from%3D8700+Marcali%2C+Pet%C5%91fi+S.+utca+11+.%26fromX%3D17.414611005499122%26fromY%3D46.57928886409497%26activeTab%3DrouteSearch&activeTab=routeSearch'" href="#">innen</a> <strong>/</strong> <span>-</span> <a onclick="window.location = '/redirect.jspv?method=Redirect&routePlanCount=11310540&url=%2Futvonalterv.jspv%3Ffrom%3D%26to%3D8700+Marcali%2C+Pet%C5%91fi+S.+utca+11+.%26toX%3D17.414611005499122%26toY%3D46.57928886409497%26activeTab%3DrouteSearch'" href="#">ide</a> </li>
            </ul>
            <div class="contacts" sizcache="1" sizset="38">
                <div class="phoneHolder clearfix" sizcache="1" sizset="38">
                    <div class="dt phone">
                        Telefonszám:
                    </div>
                    <div class="dd phoneValue tel" sizcache="1" sizset="38">
                        <span>(70) 326 2843</span> <a class="clickToCall" onclick="javascript:openECS('11310540');" href="#">Hívja ingyen!</a>
                    </div>
                </div>
                <div class="webLinkHolder" sizcache="1" sizset="39">
                    <div class="dt webLink clearfix">
                        Weboldal:
                    </div>
                    <div class="dd webLinkValue" sizcache="1" sizset="39">
                        <a href="http://www.ezerszirom.hu" rel="blank,nofollow">http://www.ezerszirom.hu</a>
                    </div>
                </div>
            </div>
        </div>
    </div>

And my code:

            results = htmlDoc.DocumentNode.SelectNodes("//div[contains(@class, 'vcard')]");
            if (results != null)
            {
                foreach (var node in results)
                {
                    Response.Write(node.SelectSingleNode("//p[@class='description']").InnerText + "<br>");
                }
            }

the problem ?
since i am iterating over the results, each node should have its own description,
but i am getting the InnerText of the first node.
any idea why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

jJeQQOZ5 2024-12-03 00:21:41

在 XPath 中,// 代表“从根开始递归搜索”。因此,查询不会从您拥有的节点开始搜索,而是从文档中获取第一个节点。

试试这个:

node.SelectSingleNode("descendant::p[@class='description']")

In XPath, // stands for "recursively search from the root". Therefore the query doesn't search from the node you have, but gets the first from the document.

Try this:

node.SelectSingleNode("descendant::p[@class='description']")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文