使用anglesharp linq查询获取Href属性 [英] Getting Href property with anglesharp linq query
问题描述
我正在尝试了解如何使用anglesharp.
I am trying to understand how to use anglesharp.
我根据示例创建了此代码( https://github.com/AngleSharp/AngleSharp ):
I made this code based on the example (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
属性,但是我无法执行此操作:
This works as expected but now I want to access the Href
property but I am unable to do this:
var links = menuItems.Select(m => m.Href).ToList();
当我在调试器中查看时,可以在结果视图中看到HtmlAnchorElement
可枚举的对象具有Href属性,但显然我没有试图正确地访问它.
When I look in the debugger I can see in results view that the HtmlAnchorElement
enumerable object has a Href property but I am obviously not trying to access it right.
文档中的所有示例均未显示正在访问的属性,因此我想它很简单,不需要显示,但我不知道如何做.
None of the examples in the documentation show a property being accessed so I guess it's something so simple that doesn't need to be shown but I am not seeing how to do it.
有人可以告诉我如何使用尖锐的角度访问html属性吗?
Can anyone show me how I should be accessing a html property with angle sharp?
当我将其转换为正确的类型时可以使用
This works when I cast it to the correct type
foreach (IHtmlAnchorElement menuLink in menuItems)
{
Console.WriteLine(menuLink.Href.ToString());
}
我如何将其写为类似于titles变量的Linq语句?
How would I write that as a Linq statement like the titles variable?
推荐答案
替代 har07的答案:
var menuItems = document.QuerySelectorAll(menuSelector).OfType<IHtmlAnchorElement>();
这篇关于使用anglesharp linq查询获取Href属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!