【问题标题】:if one html tag matches then another tag should be crawled using htmlagilitypack如果一个 html 标签匹配,则应使用 htmlagilitypack 抓取另一个标签
【发布时间】:2017-03-13 08:37:42
【问题描述】:

<a class="product-name" href="http:xyz" title="Polac pineapple slices 3kg">Polac pineapple slices 3kg</a> <div class="price-box"> <span class="regular-price" id="product-price-5489"> <span class="price">Rs 665</span> </span>

我想从 Span 标签中获取价格,但它应该在匹配时提供特定商品的价格。就像一个标签的内部文本是 Polac pineapple 那么它应该返回 665 卢比 以下是我正在使用的代码

 ` 
var aTags = document.DocumentNode.SelectNodes("//a");
                var nextTags  = document.DocumentNode.SelectNodes("//span");
if (aTags != null)
                {
                    foreach (var aTag in aTags)
                    {
                        s += counter + ".  " + aTag.InnerText + "<br>";
                        //s += aTag.InnerText;
                        if (aTag.InnerText == "Polac pineapple")
                        {
                            brandcheck = true;
                            find += aTag.InnerText + " ";

                            foreach (var nextTag in nextTags)
                            {
                                //s += counter + ".  " + nextTag.InnerText + "<br>";
                                s += nextTag.InnerText;
                                if (nextTag.InnerText.Contains("Rs"))
                                {
                                    brandcheck = true;
                                    find = nextTag.InnerText + " ";
                                }
                            }`

【问题讨论】:

    标签: c# html web-crawler html-agility-pack atag


    【解决方案1】:

    你能更精确一点吗?

    你可以使用“id”。

    <span id="thisspan">A uniquely identifiable element.</span>
    

    id 属性为文档中的元素提供唯一标识符。 a 元素可以使用它来创建指向该特定元素的超链接。

    id 属性最重要的方面是它必须是绝对唯一的。与可能对页面中的许多元素应用相同值的 class 属性不同,应用于元素的 id 不能与同一页面上其他任何地方使用的 id 匹配。

    id 属性值必须以罗马字母(a-z 或 A-Z)中的字母开头;这可以跟字母(a–z 或 A–Z)、数字 (0–9)、连字符 (-)、下划线 (_)、冒号 (:) 和句点 (.) 的任意组合。 id 值区分大小写,因此 This is me 和 This is me 将被视为同一网页上的独立且唯一可识别的元素。

    【讨论】:

    • 感谢您的回复.. 实际上有多个具有相同 id 的 span :/ 比如
    猜你喜欢
    • 2011-03-31
    • 1970-01-01
    • 2021-07-11
    • 2016-10-09
    • 1970-01-01
    • 2020-03-04
    • 2013-04-17
    • 2014-06-21
    • 1970-01-01
    相关资源
    最近更新 更多