在单个 HtmlNodeCollection c# 中获取所有 table tr 数据

Get all table tr data in single HtmlNodeCollection c#

我需要使用 HTML Agility Pack 从所有 table 中检索所有 tr

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");

我只能获得第一个 table tr 但无法获得 HtmlNodeCollection.

中的所有两个 table tr

要获取所有 tr 个节点,包括 thead 中的节点,请将您的 XPath 更新为:

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

这个简化的 XPath 也应该有效:

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

如果您只想要 tbody 中的 tr,请使用:

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

或从thead中排除tr,使用:

"//*[@id='more']//tr[not(ancestor::thead)]"