htmlagilitypack select 节点 return 空

htmlagilitypack select nodes return null

我使用此代码获取页面信息但现在站点已更改并且我的应用程序 returns 空错误。

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(page);
var query = doc.DocumentNode
  .SelectNodes("//table[@class='table table-striped table-hover']/tr")
  .Select(r => {
    return new DelegationLink()
    {
        Row = r.SelectSingleNode(".//td").InnerText,
        Category = r.SelectSingleNode(".//td[2]").InnerText
    };
}).ToList();

这是我的 html:

 <div role="tabpanel" class="tab-pane fade " id="tab3">
                <div class="circular-div">
    <table class="table table-striped table-hover" id="circular-table">
        <thead>
            <tr>
                <th>ردیف</th>
                <th>دسته بندی</th>
                <th>عنوان</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>1</td>
                <td>بخشنامه‌ها</td>
                <td>اطلاعیه جهاد دانشگاهی</td>
            </tr>
            <tr>
                <td>2</td>
                <td>بخشنامه‌ها</td>
...
...
...

我哪里错了?

Table 行不是 table 的直接后代,但它们嵌套在其他标记中,这就是您的代码返回 null 的原因。您还想跳过 header 并仅抓取 table 的 body。

var query = doc.DocumentNode
    .SelectNodes("//table[@class='table table-striped table-hover']/tbody/tr")
    .Select(r =>
    {
        return new DelegationLink()
        {
            Row = r.InnerText,
            Category = r.SelectSingleNode(".//td[2]").InnerText
        };
    }
).ToList();