我使用此代码来获取页面信息,但现在站点已更改,并且我的应用程序返回null错误。
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>
...
...
...
我哪里错了?
发布于 2018-08-12 18:05:57
表行不是表的直接后代,但它们嵌套在其他标记中,这就是代码返回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();
https://stackoverflow.com/questions/51807246
复制相似问题