我想了解如何使用 Anglesharp。
我根据示例编写了这段代码(https://github.com/AngleSharp/AngleSharp https://github.com/AngleSharp/AngleSharp):
// Setup the configuration to support document loading
var config = Configuration.Default.WithDefaultLoader();
// Load the names of all The Big Bang Theory episodes from Wikipedia
var address = "http://store.scramblestuff.com/";
// Asynchronously get the document in a new context using the configuration
var document = await BrowsingContext.New(config).OpenAsync(address);
// This CSS selector gets the desired content
var menuSelector = "#storeleft a";
// Perform the query to get all cells with the content
var menuItems = document.QuerySelectorAll(menuSelector);
// We are only interested in the text - select it with LINQ
var titles = menuItems.Select(m => m.TextContent).ToList();
var output = string.Join("\n", titles);
Console.WriteLine(output);
这按预期工作,但现在我想访问Href
财产,但我无法做到这一点:
var links = menuItems.Select(m => m.Href).ToList();
当我查看调试器时,我可以在结果视图中看到HtmlAnchorElement
可枚举对象有一个 Href 属性,但我显然并没有尝试正确访问它。
文档中的示例都没有显示正在访问的属性,所以我想这很简单,不需要显示,但我不知道如何做到这一点。
谁能告诉我应该如何使用尖角访问 html 属性?
edit:
当我将其转换为正确的类型时,这会起作用
foreach (IHtmlAnchorElement menuLink in menuItems)
{
Console.WriteLine(menuLink.Href.ToString());
}
我该如何将其写为像标题变量一样的 Linq 语句?
替代har07 的回答 https://stackoverflow.com/a/37106155/95976:
var menuItems = document.QuerySelectorAll(menuSelector).OfType<IHtmlAnchorElement>();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)