我正在尝试使用HTMLAgility解析以下内容:
<span class="button">
<a role="anotherbutton" href="/gofor/15555445554/be?ref=t">Me</a>
</span>
有这样的事情:
foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//span[@class = 'button']/a[@role = 'anotherbutton']"))
{
string att = link.Attributes["href"].Value;
txt_htmlResults.Text += att.ToString() + "\n";
}
但是我总是得到null异常...我的目的是得到15555445554.有人可以协助。提前谢谢
答案 0 :(得分:1)
把它扔到我的驱动器上的文本文件中:
HtmlDocument doc = new HtmlDocument();
doc.Load("C:\\temp\\stackhtml.html");
//string link = doc.DocumentNode.SelectSingleNode("//span[@class='button']//a").OuterHtml;
string rawLink = doc.DocumentNode.SelectSingleNode("//span[@class='button']//a").GetAttributeValue("href", "unkown");
Console.WriteLine("rawLink: " + rawLink);
string cleanedLink = rawLink.Substring(rawLink.IndexOf("r/")+2,rawLink.IndexOf("/b")-rawLink.IndexOf("r/")-2);
Console.WriteLine("cleanedLink: " + cleanedLink);
Console.ReadLine();
结果: