【发布时间】:2026-01-05 17:35:02
【问题描述】:
我需要在这个网页上阅读整个表格https://it.wikipedia.org/wiki/Episodi_di_Watchmen
我不关心标题,但我当然需要阅读每一行和每一列。我写了这段代码:
string page = "https://it.wikipedia.org/wiki/Episodi_di_Watchmen";
HtmlDocument doc = new HtmlDocument();
StreamReader reader = new StreamReader(WebRequest.Create(page).GetResponse().GetResponseStream(), Encoding.UTF8);
doc.Load(reader);
List<List<string>> table = doc.DocumentNode.SelectSingleNode("//table[@class='wikitable']")
.Descendants("tr").Select(x => x.ChildNodes.Select(c => c.InnerText.Trim())
.Where(y => !string.IsNullOrWhiteSpace(y)).ToList()).ToList();
不幸的是,上面的代码在列表行上给出了一个错误。做了一些试验,似乎错误出在 Descendants 方法中。
你能帮帮我吗?
【问题讨论】:
-
您应该使用Wikimedia API 而不是试图抓取*页面。
-
我开始认为我必须使用 Wikimedia API,因为我的代码直到几天前才有效。我需要研究它,我从来没有用过这样的东西,
标签: c# html .net html-agility-pack