HtmlNode 从嵌套范围获取内部文本

HtmlNode Get inner text from nested span

我正在尝试从 html 段获取信息,一切进展顺利,但我正在努力 return Trade in 值的值。下面是我到目前为止尝试过的代码的副本。

htmlNode.Descendants("li").Where(x => x.HasClass("trade-in-price")).Select(x => x.Descendants("span").Where(z => z.HasClass("value")).Last().InnerText);

其中 return 如下:

"£36.00"

现在,我真的不想对这个值进行子字符串化以获得成本,因为我认为这不是最好的方法,但是我已经尝试了所有其他方法,但我似乎无法 return 'just the cost' 值。

这是 html 我正在尝试导航以获得所需值的副本

            <section
                class="product-item"
                itemscope="itemscope">
                <div>
                    <div class="group">
                        <div>
                            <div class="product-image"><a
                                href="/trade-in-sell/call-of-duty-modern-warfare-ps4"
                                itemprop="url"
                            ><span><img
                                width="160"
                                height="200"
                                alt="Call Of Duty: Modern Warfare"
                                title="Show more information on Call Of Duty: Modern Warfare"
                                itemprop="image"
                            /></span></a></div>
                            <div class="product-categories gray">
                                <ul>
                                    <li>PlayStation</li>
                                </ul>
                            </div>
                            <div class="product-label top-seller"><strong>modernwarfare</strong></div>
                            <h2 class="product-title" itemprop="name">Call Of Duty: Modern Warfare</h2>
                        </div>
                    </div>
                    <div class="group">
                        <div>
                            <div class="product-price">
                                <ul>
                                    <li class="buy-new-price">
                                        <Buy new</span> <span class="value"><span class="symbol l">&pound;</span>49.99</span>
                                    </li>
                                    <li class="trade-in-price">
                                        <a href="/trade-in-sell/call-of-duty-modern-warfare-ps4">
                                            <span class="label">Trade in</span> 
                                            <span class="value">
                                                <span class="symbol l">
                                                    &pound;
                                                </span>
                                                36.00   // I want this value here
                                            </span>
                                        </a>
                                    </li>
                                    <li class="sell-price">
                                        <a href="/trade-in-sell/call-of-duty-modern-warfare-ps4">
                                            <span class="label">Get cash</span> 
                                            <span class="value">
                                                <span class="symbol l">
                                                    &pound;
                                                </span>
                                                32.00
                                            </span>
                                        </a>
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
            </section>

有谁知道我的 LINQ 查询哪里出错了?

我认为您可以使用方法 GetDirectInnerText() 而不是 属性 InnerText。对我来说,它 returns 只有节点本身没有孩子的文本。

htmlNode.Descendants("li").Where(x => x.HasClass("trade-in-price")).Select(x => x.Descendants("span").Where(z => z.HasClass("value")).Last().GetDirectInnerText());