我开始在一个项目中使用 Anglesharp,我不仅需要获取和下载 HTML,还需要获取和下载文档的图像。我知道在 Document 对象中有一个名为 Images 的属性,但似乎它没有得到所有这些,我在 YouTube 页面上做了一个测试,只得到了一个(重复了几次)。例如,我想获取当前视频的缩略图,这似乎在<meta>标签内。更准确地说,图像存储在这种标签中:
<meta content="https://i.ytimg.com/vi/hW-kDv1WcQM/hqdefault.jpg" property="og:image">
Run Code Online (Sandbox Code Playgroud)
所以我想知道是否有一种方法可以选择页面内任何图像的所有节点/url,无论使用什么标签。我认为 QuerySelectorAll 在这种情况下不起作用,因为它只选择一种类型的节点。您可以尝试在 github 上找到的示例代码来验证这一点(我只是用 YouTube 更改了 url,也更改了选择器 :D):
// 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 = "https://www.youtube.com/watch?v=hW-kDv1WcQM&feature=youtu.be";
// 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 cellSelector = …Run Code Online (Sandbox Code Playgroud)