【发布时间】:2016-11-17 21:07:00
【问题描述】:
我正在尝试解析 HTML 文档以检索页面中的特定链接。我知道这可能不是最好的方法,但我试图通过其内部文本找到我需要的 HTML 节点。但是,在 HTML 中有两个实例会发生这种情况:页脚和导航栏。我需要导航栏中的链接。 HTML 中的“页脚”首先出现。这是我的代码:
public string findCollegeURL(string catalog, string college)
{
//Find college
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(catalog);
var root = doc.DocumentNode;
var htmlNodes = root.DescendantsAndSelf();
// Search through fetched html nodes for relevant information
int counter = 0;
foreach (HtmlNode node in htmlNodes) {
string linkName = node.InnerText;
if (linkName == colleges[college] && counter == 0)
{
counter++;
continue;
}
else if(linkName == colleges[college] && counter == 1)
{
string targetURL = node.Attributes["href"].Value; //"found it!"; //
return targetURL;
}/* */
}
return "DID NOT WORK";
}
程序正在进入 if else 语句,但在尝试检索链接时,我收到 NullReferenceException。 这是为什么呢?如何检索我需要的链接?
这是我试图访问的 HTML 文档中的代码:
<tr class>
<td id="acalog-navigation">
<div class="n2_links" id="gateway-nav-current">...</div>
<div class="n2_links">...</div>
<div class="n2_links">...</div>
<div class="n2_links">...</div>
<div class="n2_links">...</div>
<a href="/content.php?catoid=10&navoid=1210" class"navbar" tabindex="119">College of Science</a> ==$0
</div>
这是我想要的链接:/content.php?catoid=10&navoid=1210
【问题讨论】:
标签: c# html html-parsing html-agility-pack