使用 HTML 敏捷包获取 HTML 页面内的 URL
Get URLs inside a HTML page with HTML Agility Pack
我有这个代码:
foreach (HtmlNode node in hd.DocumentNode.SelectNodes("//div[@class='compTitle options-toggle']//a"))
{
string s=("node:" + node.GetAttributeValue("href", string.Empty));
}
我想在这样的标签中获取网址:
<div class="compTitle options-toggle">
<a class=" ac-algo fz-l ac-21th lh-24" href="http://www.bestbuy.com">
<b>Huawei</b> Products - Best Buy
</a>
</div>
我想获得“http://www.bestbuy.com”和“华为产品-百思买”
我该怎么办?我的代码正确吗?
这是工作代码的示例
var document = new HtmlDocument();
document.LoadHtml("<div class=\"compTitle options-toggle\"><a class=\" ac-algo fz-l ac-21th lh-24\" href=\"http://www.bestbuy.com\"><b>Huawei</b> Products - Best Buy</a></div>");
var tags = document.DocumentNode.SelectNodes("//div[@class='compTitle options-toggle']//a").ToList();
foreach (var tag in tags)
{
var link = tag.Attributes["href"].Value; // http://www.bestbuy.com
var text = tag.InnerText; // Huawei Products - Best Buy
}
结束双引号应该修复选择(对我有用)。
获取纯文本为
string contentText = node.InnerText;
或者把Huawei字样加粗,像这样:
string contentHtml = node.InnerHtml;
我有这个代码:
foreach (HtmlNode node in hd.DocumentNode.SelectNodes("//div[@class='compTitle options-toggle']//a"))
{
string s=("node:" + node.GetAttributeValue("href", string.Empty));
}
我想在这样的标签中获取网址:
<div class="compTitle options-toggle">
<a class=" ac-algo fz-l ac-21th lh-24" href="http://www.bestbuy.com">
<b>Huawei</b> Products - Best Buy
</a>
</div>
我想获得“http://www.bestbuy.com”和“华为产品-百思买”
我该怎么办?我的代码正确吗?
这是工作代码的示例
var document = new HtmlDocument();
document.LoadHtml("<div class=\"compTitle options-toggle\"><a class=\" ac-algo fz-l ac-21th lh-24\" href=\"http://www.bestbuy.com\"><b>Huawei</b> Products - Best Buy</a></div>");
var tags = document.DocumentNode.SelectNodes("//div[@class='compTitle options-toggle']//a").ToList();
foreach (var tag in tags)
{
var link = tag.Attributes["href"].Value; // http://www.bestbuy.com
var text = tag.InnerText; // Huawei Products - Best Buy
}
结束双引号应该修复选择(对我有用)。
获取纯文本为
string contentText = node.InnerText;
或者把Huawei字样加粗,像这样:
string contentHtml = node.InnerHtml;