使用 HtmlAgilityPack C# 检索特定 URL
Retrieving specific URLs with HtmlAgilityPack C#
我目前正在尝试使用 HtmlAgilityPack 从 html 页面中提取特定的 link。我尝试使用纯 C# 强制进入,但事实证明这真的很痛苦。 link 都在具有相同 class 的 <div>
标签内。这是我拥有的:
HtmlWeb web = new HtmlWeb();
HtmlDocument html = web.Load(url);
//this should select only the <div> tags with the class acTrigger
foreach (HtmlNode node in html.DocumentNode.SelectNodes("//div[@class='acTrigger']"))
{
//not sure how to dig further in to get the href values from each of the <a> tags
}
站点代码看起来与此
一致
<li>
<div class="acTrigger">
<a href="/16014988/d/" onclick="return queueRefinementAnalytics('Category','Battery')">
Battery <em> (1)</em>
</a>
</div>
</li>
<li>
<div class="acTrigger">
<a href="/15568540/d/" onclick="return queueRefinementAnalytics('Category','Brakes')">
Brakes <em> (2)</em>
</a>
</div>
</li>
<li>
<div class="acTrigger">
<a href="/11436914/d/1979-honda-ct90-cables-lines" onclick="return queueRefinementAnalytics('Category','Cables/Lines')">
Cables/Lines <em> (1)</em>
</a>
</div>
</li>
这个页面上有很多 link,但是我需要的 href
包含在嵌套在 [=15= 中的那些 <a>
标签中] 标签。如果每个 <a>
共享唯一的 classes 会很简单,但不幸的是只有 <div>
标签有 classes。我需要做的是抓取每个 href
并存储它们,以便我以后可以检索它们,转到每个页面,并从每个页面检索更多信息。我只需要朝着正确的方向轻推以克服这个困难,然后我也应该能够完成其他页面。我以前没有使用此 HtmlAgilityPack 的经验,我发现的所有示例似乎都想从页面中提取所有 URL,而不是特定的 URL。我只需要 link 示例或文档,非常感谢任何帮助。
您应该可以更改 select 以包含 <a>
标签://div[@class='acTrigger']/a
。这样你的 HtmlNode
就是你的 <a>
标签而不是 div.
要存储链接,您可以使用 GetAttributeValue
。
foreach (HtmlNode node in html.DocumentNode.SelectNodes("//div[@class='acTrigger']/a"))
{
// Get the value of the HREF attribute.
string hrefValue = node.GetAttributeValue( "href", string.Empty );
// Then store hrefValue for later.
}
我目前正在尝试使用 HtmlAgilityPack 从 html 页面中提取特定的 link。我尝试使用纯 C# 强制进入,但事实证明这真的很痛苦。 link 都在具有相同 class 的 <div>
标签内。这是我拥有的:
HtmlWeb web = new HtmlWeb();
HtmlDocument html = web.Load(url);
//this should select only the <div> tags with the class acTrigger
foreach (HtmlNode node in html.DocumentNode.SelectNodes("//div[@class='acTrigger']"))
{
//not sure how to dig further in to get the href values from each of the <a> tags
}
站点代码看起来与此
一致 <li>
<div class="acTrigger">
<a href="/16014988/d/" onclick="return queueRefinementAnalytics('Category','Battery')">
Battery <em> (1)</em>
</a>
</div>
</li>
<li>
<div class="acTrigger">
<a href="/15568540/d/" onclick="return queueRefinementAnalytics('Category','Brakes')">
Brakes <em> (2)</em>
</a>
</div>
</li>
<li>
<div class="acTrigger">
<a href="/11436914/d/1979-honda-ct90-cables-lines" onclick="return queueRefinementAnalytics('Category','Cables/Lines')">
Cables/Lines <em> (1)</em>
</a>
</div>
</li>
这个页面上有很多 link,但是我需要的 href
包含在嵌套在 [=15= 中的那些 <a>
标签中] 标签。如果每个 <a>
共享唯一的 classes 会很简单,但不幸的是只有 <div>
标签有 classes。我需要做的是抓取每个 href
并存储它们,以便我以后可以检索它们,转到每个页面,并从每个页面检索更多信息。我只需要朝着正确的方向轻推以克服这个困难,然后我也应该能够完成其他页面。我以前没有使用此 HtmlAgilityPack 的经验,我发现的所有示例似乎都想从页面中提取所有 URL,而不是特定的 URL。我只需要 link 示例或文档,非常感谢任何帮助。
您应该可以更改 select 以包含 <a>
标签://div[@class='acTrigger']/a
。这样你的 HtmlNode
就是你的 <a>
标签而不是 div.
要存储链接,您可以使用 GetAttributeValue
。
foreach (HtmlNode node in html.DocumentNode.SelectNodes("//div[@class='acTrigger']/a"))
{
// Get the value of the HREF attribute.
string hrefValue = node.GetAttributeValue( "href", string.Empty );
// Then store hrefValue for later.
}