【发布时间】:2018-01-01 01:52:10
【问题描述】:
我正在尝试在 C# 中使用 HtmlAgilityPack 解析 HTML。我有21 tr items,每个 tr 项目都有7 td items。如何按顺序获取所有 tr 和 td 项目?现在我只能得到一个 tr 项目和它的 7 个 td 项目。
这是我的 C# 代码:
var url = "url";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
string sourceCode = sr.ReadToEnd();
HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
document.LoadHtml(sourceCode);
var name = document.DocumentNode.SelectNodes("//*[@id=\"searchResultsTable\"]/tbody/tr[1]/td[2]/a[1]")[0].InnerText;
var year = document.DocumentNode.SelectNodes("//*[@id=\"searchResultsTable\"]/tbody/tr[1]/td[3]")[0].InnerText;
var km = document.DocumentNode.SelectNodes("//*[@id=\"searchResultsTable\"]/tbody/tr[1]/td[4]")[0].InnerText;
var color = document.DocumentNode.SelectNodes("//*[@id=\"searchResultsTable\"]/tbody/tr[1]/td[5]")[0].InnerText;
var price = document.DocumentNode.SelectNodes("//*[@id=\"searchResultsTable\"]/tbody/tr[1]/td[6]")[0].InnerText;
var date = document.DocumentNode.SelectNodes("//*[@id=\"searchResultsTable\"]/tbody/tr[1]/td[7]")[0].InnerText;
var location = document.DocumentNode.SelectNodes("//*[@id=\"searchResultsTable\"]/tbody/tr[1]/td[8]")[0].InnerText;
我尝试使用[@id=\"searchResultsTable\"]/tbody/tr[1]/td[position()<8],但只返回 /n
【问题讨论】:
标签: c# html xpath html-agility-pack