【发布时间】:2016-07-01 15:19:55
【问题描述】:
我开始在项目中使用 Anglesharp,我不仅需要获取和下载 HTML,还需要获取和下载文档的图像。
我知道在 Document 对象中有一个名为 Images 的属性,但显然它并没有得到所有这些,我在 YouTube 页面上做了一个测试,结果只有一个(重复了几次)。
例如,我想获取当前视频的缩略图,这似乎在 <meta> 标签内。
更准确地说,图像存储在这种标签中:
<meta content="https://i.ytimg.com/vi/hW-kDv1WcQM/hqdefault.jpg" property="og:image">
所以我想知道是否有一种方法可以选择页面内任何图像的所有节点/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 = "img";
// Perform the query to get all cells with the content
var cells = document.QuerySelectorAll(cellSelector);
// We are only interested in the text - select it with LINQ
var titles = cells.Select(m => m.TextContent);
哦,舒尔,你也可以添加这个来检查 Image 属性是否没有获取视频缩略图:
var Images = document.Images.Select(sl=> sl.Source).Distinct().ToList();
还有其他方法可以根据 URL 内容选择节点吗? (就像所有以“.jpg”或“.png”等结尾的网址)
【问题讨论】:
-
我做了,但是 Anglesharp 没有很多文档(如果你在 stackexchage 中查找 Anglesharp 标签,你会得到 17 个(包括我的)结果:stackoverflow.com/questions/tagged/anglesharp)。谷歌搜索也无济于事,您可以尝试几个搜索词,但对于 AngleShapr,没有多少。
-
你得到了获取图片的最终解决方案吗?
标签: c# html image selectors-api anglesharp