使用 anglesharp linq 查询获取 Href 属性
Getting Href property with anglesharp linq query
我正在尝试了解如何使用 anglessharp。
我根据示例 (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 属性,但我显然没有尝试正确访问它。
None 文档中的示例显示正在访问 属性,所以我想这很简单,不需要显示,但我不知道如何去做。
任何人都可以告诉我应该如何使用锐角访问 html 属性 吗?
编辑:
当我将其转换为正确的类型时这有效
foreach (IHtmlAnchorElement menuLink in menuItems)
{
Console.WriteLine(menuLink.Href.ToString());
}
我如何将它写成像 titles 变量一样的 Linq 语句?
您可以转换为 IHtmlAnchorElement
,如下所示:
var links = menuItems.Select(m => ((IHtmlAnchorElement)m).Href).ToList();
或使用Cast<IHtmlAnchorElement>()
:
var links = menuItems.Cast<IHtmlAnchorElement>()
.Select(m => m.Href)
.ToList();
替代:
var menuItems = document.QuerySelectorAll(menuSelector).OfType<IHtmlAnchorElement>();
我对这个话题有点晚了,但你可以使用
string link = menuItem.GetAttribute("href");
如果它是项目列表,则为这个
List<string> menuItems = LinkList.Select(item => item.GetAttribute("href")) .ToList();
我正在尝试了解如何使用 anglessharp。
我根据示例 (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 属性,但我显然没有尝试正确访问它。
None 文档中的示例显示正在访问 属性,所以我想这很简单,不需要显示,但我不知道如何去做。
任何人都可以告诉我应该如何使用锐角访问 html 属性 吗?
编辑:
当我将其转换为正确的类型时这有效
foreach (IHtmlAnchorElement menuLink in menuItems)
{
Console.WriteLine(menuLink.Href.ToString());
}
我如何将它写成像 titles 变量一样的 Linq 语句?
您可以转换为 IHtmlAnchorElement
,如下所示:
var links = menuItems.Select(m => ((IHtmlAnchorElement)m).Href).ToList();
或使用Cast<IHtmlAnchorElement>()
:
var links = menuItems.Cast<IHtmlAnchorElement>()
.Select(m => m.Href)
.ToList();
替代
var menuItems = document.QuerySelectorAll(menuSelector).OfType<IHtmlAnchorElement>();
我对这个话题有点晚了,但你可以使用
string link = menuItem.GetAttribute("href");
如果它是项目列表,则为这个
List<string> menuItems = LinkList.Select(item => item.GetAttribute("href")) .ToList();