HtmlAgilityPack從html頁面讀取數據

c# html-agility-pack

我正在嘗試使用html agility pack從html表中獲取數據,但只保留第一個表行中的數據。

我正在閱讀的HTML代碼如下:

<div id="mainDiv">
    <table id="tbl">
        <thead>
            <tr>
                <th class="tbl_col1">UserName</th>
                <th class="tbl_col2">Points</th>
            </tr>
        </thead>
        <tbody>     
          <tr data-source="provider1">
            <td class="tbl_col1">
                <a href="/Users/1090" id="UserLink" target="_blank">UserName1</a>           
            </td>
            <td class="tbl_col2">
                <a href="/UserPoints/1090" id="PointLink" target="_blank">1892 <span class="up_arrow">&nbsp;</span></a>             
            </td>           
          </tr>
          <tr data-source="provider2">
            <td class="tbl_col1">
                <a href="/Users/1090" id="UserLink" target="_blank">UserName2</a>           
            </td>
            <td class="tbl_col2">
                <a href="/UserPoints/1090" id="PointLink" target="_blank">3217 <span class="down_arrow">&nbsp;</span></a>               
            </td>           
         </tr>
        </tbody>
    </table>
</div>  

我正在使用此代碼

<div id="mainDiv">
    <table id="tbl">
        <thead>
            <tr>
                <th class="tbl_col1">UserName</th>
                <th class="tbl_col2">Points</th>
            </tr>
        </thead>
        <tbody>     
          <tr data-source="provider1">
            <td class="tbl_col1">
                <a href="/Users/1090" id="UserLink" target="_blank">UserName1</a>           
            </td>
            <td class="tbl_col2">
                <a href="/UserPoints/1090" id="PointLink" target="_blank">1892 <span class="up_arrow">&nbsp;</span></a>             
            </td>           
          </tr>
          <tr data-source="provider2">
            <td class="tbl_col1">
                <a href="/Users/1090" id="UserLink" target="_blank">UserName2</a>           
            </td>
            <td class="tbl_col2">
                <a href="/UserPoints/1090" id="PointLink" target="_blank">3217 <span class="down_arrow">&nbsp;</span></a>               
            </td>           
         </tr>
        </tbody>
    </table>
</div>  

但我一直得到這個輸出:

<div id="mainDiv">
    <table id="tbl">
        <thead>
            <tr>
                <th class="tbl_col1">UserName</th>
                <th class="tbl_col2">Points</th>
            </tr>
        </thead>
        <tbody>     
          <tr data-source="provider1">
            <td class="tbl_col1">
                <a href="/Users/1090" id="UserLink" target="_blank">UserName1</a>           
            </td>
            <td class="tbl_col2">
                <a href="/UserPoints/1090" id="PointLink" target="_blank">1892 <span class="up_arrow">&nbsp;</span></a>             
            </td>           
          </tr>
          <tr data-source="provider2">
            <td class="tbl_col1">
                <a href="/Users/1090" id="UserLink" target="_blank">UserName2</a>           
            </td>
            <td class="tbl_col2">
                <a href="/UserPoints/1090" id="PointLink" target="_blank">3217 <span class="down_arrow">&nbsp;</span></a>               
            </td>           
         </tr>
        </tbody>
    </table>
</div>  

一般承認的答案

您做出了錯誤的假設: //a[@id='UserLink']/text()//a[@id='PointLink']/text()搜索整個文檔。這就是你獲得第一個tr節點的原因。只需使用:

string UserName = row.SelectSingleNode("td[@class='tbl_col1']/a[@id='UserLink']/text()").InnerText;
string Points = row.SelectSingleNode("td[@class='tbl_col2']/a[@id='PointLink']/text()").InnerText;

您還可以真正簡化其餘代碼:

string UserName = row.SelectSingleNode("td[@class='tbl_col1']/a[@id='UserLink']/text()").InnerText;
string Points = row.SelectSingleNode("td[@class='tbl_col2']/a[@id='PointLink']/text()").InnerText;



許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因