gpt4 book ai didi

c# - htmlagilitypack 选择节点返回空

转载 作者:太空宇宙 更新时间:2023-11-03 22:41:50 29 4
gpt4 key购买 nike

我使用这段代码来获取页面信息但是现在站点已经更改并且我的应用程序返回空错误。

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

我哪里错了?

最佳答案

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

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

关于c# - htmlagilitypack 选择节点返回空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51807246/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com