簡體   English   中英

HtmlNode 從嵌套跨度中獲取內部文本

[英]HtmlNode Get inner text from nested span

我正在嘗試從 html 段獲取信息,一切順利,但是我正在努力返回價值Trade in價值。 下面是我迄今為止嘗試過的代碼的副本。

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

它返回以下內容:

"£36.00"

現在,我真的不想用 substring 這個值來獲得成本,因為我認為這不是最好的方法,但是我已經嘗試了所有其他方式,我似乎無法返回“只是成本”價值。

這是 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 對我來說,它只返回節點本身的文本,沒有子節點。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM