Get all table tr data in single HtmlNodeCollection c#

c# html-agility-pack web-scraping

Question

I must get everything backtr using HTML Agility Pack, from all tables.

HTML:

<section class="content-section" id="more">
    <div class="row">
        <div class="col-xs-6"></div>
        <div class="col-xs-6">
            <h2>Specs</h2>
            <div>
                <div>
                    <table>
                        <thead>
                            <tr><th colspan="2"> test</th></tr>
                        </thead>
                        <tbody>                         
                            <tr><td>2</td><td>b</td></tr>
                            <tr><td>1</td><td>a</td></tr>
                        </tbody>
                    </table>                                    
                    <table>
                        <tbody>
                            <tr><td>3</td><td>c</td></tr>
                            <tr><td>4</td><td>d</td></tr>               
                        </tbody>
                    </table>
                </div>              
            </div>
        </div>      
    </div>  
</section>

C#:

HtmlNodeCollection featuresNode = document.DocumentNode.SelectNodes("//*[@id='more']/div/div[2]/div/div[1]/table/tbody/tr");

Only the first table is available to me.tr but not receiving both tablestr in HtmlNodeCollection .

1
1
9/13/2018 10:23:17 AM

Accepted Answer

to completetr nodes, such as that inthead , revise yourXPath to:

"//*[@id='more']/div/div[2]/div/div[1]/table//tr"

This condensedXPath likewise be effective

"//*[@id='more']//tr"

If you merely want thetr from tbody , use:

"//*[@id='more']//tbody//tr"

or by omittingtr from thead , use:

"//*[@id='more']//tr[not(ancestor::thead)]"
2
9/13/2018 10:17:12 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow