【问题标题】:HtmlAgilityPack filtering HTML based on a queryHtmlAgilityPack 基于查询过滤 HTML
【发布时间】:2017-09-05 19:23:34
【问题描述】:

我有一个由两个 HTML 元素组成的块,如下所示:

<div class="a-row">
    <a class="a-size-small a-link-normal a-text-normal" href="/Chemical-Guys-CWS-107-Extreme-Synthetic/dp/B003U4P3U0/ref=sr_1_1_sns?s=automotive&amp;ie=UTF8&amp;qid=1504525216&amp;sr=1-1">
        <span aria-label="$19.51" class="a-color-base sx-zero-spacing">
            <span class="sx-price sx-price-large">
                <sup class="sx-price-currency">$</sup>
                <span class="sx-price-whole">19</span>
                <sup class="sx-price-fractional">51</sup>
            </span>
        </span>
        <span class="a-letter-space"></span>Subscribe &amp; Save
    </a>
</div>

以及下一个 HTML 块:

<div class="a-row a-spacing-none">
    <a class="a-link-normal a-text-normal" href="https://rads.stackoverflow.com/amzn/click/com/B003U4P3U0" rel="nofollow noreferrer">
        <span aria-label="$22.95" class="a-color-base sx-zero-spacing">
            <span class="sx-price sx-price-large">
                <sup class="sx-price-currency">$</sup>
                <span class="sx-price-whole">22</span>
                <sup class="sx-price-fractional">95</sup>
            </span>
         </span>
    </a>
    <span class="a-letter-space"></span>
    <i class="a-icon a-icon-prime a-icon-small s-align-text-bottom" aria-label="Prime">
        <span class="a-icon-alt">Prime</span>
    </i>
</div>

这两个元素的结构非常相似,但诀窍是我想提取元素的值,它旁边包含一个带有类的 span 元素:aria-label="Prime"

这是我目前提取价格的方式,但效果不佳:

if (htmlDoc.DocumentNode.SelectNodes("//span[@class='a-color-base sx-zero-spacing']") != null)
{
    var span = htmlDoc.DocumentNode.SelectSingleNode("//span[@class='a-color-base sx-zero-spacing']");
    price = span.Attributes["aria-label"].Value;
}

这基本上选择了位置 0 的 HTML 元素,因为有多个元素。但这里的诀窍是我想选择包含 prime value 的 span 元素,就像我展示的第二段 HTML 一样...... 如果不存在具有此类值的第二个元素,我只需使用我在那里写的第一种方法...

有人可以帮我解决这个问题吗? =)

我也试过这样的:

 var pr = htmlDoc.DocumentNode.SelectNodes("//a[@class='a-link-normal a-text-normal']")
    .Where(x => x.SelectSingleNode("//i[@class='a-icon a-icon-prime a-icon-small s-align-text-bottom']") != null)
    .Select(x => x.SelectSingleNode("//span[@class='a-color-base sx-zero-spacing']").Attributes["aria-label"].Value);

但它仍然返回第一个元素 xD

新版本的家伙:

 var pr = htmlDoc.DocumentNode.SelectNodes("//a[@class='a-link-normal a-text-normal']");
 string prrrrrr = "";
 for (int i = 0; i < pr.Count; i++)
   {
    if (pr.ElementAt(i).SelectNodes("//i[@class='a-icon a-icon-prime a-icon-small s-align-text-bottom']").ElementAt(i) != null)
   {
    prrrrrr = pr.ElementAt(i).SelectNodes("//span[@class='a-color-base sx-zero-spacing']").ElementAt(i).Attributes["aria-label"].Value;

    }
}

所以我的想法是,我从 HTML 文件中取出所有“a”元素并创建一个 a 的 HTML 节点集合,然后遍历它们,看看哪个确实包含我正在寻找的元素,然后匹配它...?

这里的问题是这个 if 语句总是通过:

 if (pr.ElementAt(i).SelectNodes("//i[@class='a-icon a-icon-prime a-icon-small s-align-text-bottom']").ElementAt(i) != null)

如何循环遍历节点集合中的每个单独元素?

【问题讨论】:

  • @Stephen Muecke 刚刚在问题中添加了另一段代码 =D
  • 有人吗? =)

标签: c# html html-agility-pack


【解决方案1】:

我认为您应该开始查看div 级别和a-row 级别。然后循环检查div 是否包含iarea-label 等于'Prime'。最后得到带有a-color-base sx-zero-spacing 类的spanaria-label 属性的值,如下所示:

HtmlNodeCollection nodes = htmlDoc.DocumentNode.SelectNodes("//div[starts-with(@class,'a-row')]");

foreach (HtmlNode node in nodes)
{
    HtmlNode i = node.SelectSingleNode("i[@aria-label='Prime']");

    if (i != null)
    {
        HtmlNode span = node.SelectSingleNode(".//span[@class='a-color-base sx-zero-spacing']");

        if (span != null)
        {
            string currentValue = span.Attributes["aria-label"].Value;
        }
    }
}

【讨论】:

    猜你喜欢
    • 2018-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-14
    • 2016-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多