简体   繁体   中英

HtmlNode Get inner text from nested span

I am trying to get information from a html segment, it is all going well however I am struggling to return the value of the Trade in value. Below is a copy of the code I have tried so far.

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

which returns the following:

"£36.00"

Now, I don't really want to substring this value to get the cost as I don't think it is the best way to do this however I have tried every other way and i can't seem to return 'just the cost' value.

Here is a copy of the html I am trying to navigate to get the desired value

            <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>

Does anyone know where abouts I am going wrong in my LINQ query?

I think you can use the method GetDirectInnerText() instead of property InnerText . For me it returns only text of node itself without childs.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM