Http 敏捷包 - 访问兄弟姐妹?

发布于 2024-09-29 11:44:29 字数 591 浏览 2 评论 0原文

使用 HTML Agility Pack 非常适合获取后代和整个表格等...但是在下面的情况下如何使用它你怎么能

...Html Code above...

<dl>
<dt>Location:</dt>
<dd>City, London</dd>
<dt style="padding-bottom:10px;">Distance:</dt>
<dd style="padding-bottom:10px;">0 miles</dd>
<dt>Date Issued:</dt>
<dd>26/10/2010</dd>
<dt>type:</dt>
<dd>cement</dd>
</dl>

...HTML Code below....

找到如果在这种情况下英里小于 15,我不明白你可以对元素做一些事情,但是您是否必须让所有元素找到正确的元素,然后找到数字只是为了检查其值?或者有没有办法将正则表达式与 Agility pack 一起使用以更好的方式实现这一目标......

Using the HTML Agility Pack is great for getting descendants and whole tables etc... but how can you use it in the below situation

...Html Code above...

<dl>
<dt>Location:</dt>
<dd>City, London</dd>
<dt style="padding-bottom:10px;">Distance:</dt>
<dd style="padding-bottom:10px;">0 miles</dd>
<dt>Date Issued:</dt>
<dd>26/10/2010</dd>
<dt>type:</dt>
<dd>cement</dd>
</dl>

...HTML Code below....

How could you find If miles was less than 15 in this case, I undestand you could do something with elements but would you have to get all elements find the correct one and then find the number just to check its value? Or is there are way to use regex with Agility pack to achieve this in a better way...

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

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

发布评论

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

评论(2

想挽留 2024-10-06 11:44:29

我非常确定(尚未检查)它支持 following-sibling:: 轴,因此您可以任一找到节点 "dt[. ='Distance:']" 然后找到 node.SelectSingleNode("following-sibling::dd[1]") - 或者(更简单)只使用 node.NextSibling 如果您确定 dd 始终紧跟在 dt 后面。

例如:

string distance = doc.DocumentNode.SelectSingleNode(
          "//dt[.='Distance:']/following-sibling::dd").InnerText;

I'm pretty sure (haven't checked) that it supports the following-sibling:: axis, so you could either find the node "dt[.='Distance:']" and then find node.SelectSingleNode("following-sibling::dd[1]") - or (simpler) just use node.NextSibling if you are sure that the dd always immediately follows the dt.

For example:

string distance = doc.DocumentNode.SelectSingleNode(
          "//dt[.='Distance:']/following-sibling::dd").InnerText;
怎言笑 2024-10-06 11:44:29

只获取 html 兄弟姐妹

public static List<HtmlNode> GetHtmlNodeList(string html)
{
        HtmlDocument doc = new HtmlDocument();
        doc.LoadHtml(html);
        var regs = doc.DocumentNode.SelectSingleNode("//div");
        var first = regs.Descendants().FirstOrDefault();
        var second = first.NextSibling;
        List<HtmlNode> list = new List<HtmlNode>();
        while (second != null)
        {
            list.Add(second);
            second = CheckSibling(second);
        }
        return list;
}

private static HtmlNode CheckSibling(HtmlNode node)
{
        node = node.NextSibling;
        return node;          
}

Get just html siblings

public static List<HtmlNode> GetHtmlNodeList(string html)
{
        HtmlDocument doc = new HtmlDocument();
        doc.LoadHtml(html);
        var regs = doc.DocumentNode.SelectSingleNode("//div");
        var first = regs.Descendants().FirstOrDefault();
        var second = first.NextSibling;
        List<HtmlNode> list = new List<HtmlNode>();
        while (second != null)
        {
            list.Add(second);
            second = CheckSibling(second);
        }
        return list;
}

private static HtmlNode CheckSibling(HtmlNode node)
{
        node = node.NextSibling;
        return node;          
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文