HTMLAgilityPack获取带有id属性的td标签的innerText
问题描述:
我正在尝试使用HTMLAgilityPack选择具有id属性的td的内部文本.
I am trying to select the inner text of a td with an id attribute with the HTMLAgilityPack.
HTML代码:
<td id="header1"> 5 </td>
<td id="header2"> 8:39pm </td>
<td id="header3"> 8:58pm </td>
...
代码:
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(data);
var nodes = doc.DocumentNode.SelectNodes("//td[@id='header1']");
if (nodes != null)
{
foreach (HtmlAgilityPack.HtmlNode node in nodes)
{
MessageBox.Show(node.InnerText);
}
}
我一直得到空节点,因为我没有正确选择td标签,但无法弄清楚我做错了什么...
I keep getting null nodes because I am not selecting the td tag correctly but cannot figure out what I have done wrong...
我在header1和header2上弄错了,但是有5个不同的td标签,它们的标题为1到5.
I made a mistake with header1 and header2, but there are 5 different td tags with headers 1 to 5.
答
您正在尝试选择header1
,但ID为header2
.
You are trying to select header1
but the id is header2
.
您也可以直接使用GetElementById
:
var td = doc.GetElementbyId("header2");