[c#]LINQ 来获取类型的后裔"a"从 HTMLNodesCollection

标签: Linq .Net C#
发布时间: 2017/2/28 22:53:47
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有 colectionof HTML 节点︰

 var imageColletion = htmlDoc.DocumentNode.Descendants("div").Where(d =>     
d.Attributes.Contains("Class") && d.Attributes["Class"].Value.
Contains("entry-thumbnail hover-thumb")).Select(x => x.ChildNodes);

现在我想从 imageColletion 得到"a"。尝试与

    var allImages = imageColletion.Select(nodeCollection => 
  nodeCollection.Descendants("a"));

在这里我没获得任何产出。我的查询有何不妥?

enter image description here

EDIT1: HtmlAgilityPack.HtmlDocument htmlDoc = 新 HtmlWeb()。Load(url);

  var headerCollection = htmlDoc.DocumentNode.Descendants("h2").Where(d => d.Attributes.Contains("Class") && d.Attributes["Class"].Value.Contains("entry-title")).Select(x => x.ChildNodes);
  var titleCollection = headerCollection.Select(x => x.Select(y => y.InnerHtml).Where(strValue=>strValue!="\n").FirstOrDefault());

 var imageColletion = htmlDoc.DocumentNode.Descendants("div").Where(d => d.Attributes.Contains("Class") && d.Attributes["Class"].Value.Contains("entry-thumbnail hover-thumb")).Select(x => x.ChildNodes);
                //var allImages = imageColletion.Select(nodeCollection => nodeCollection.Select(x => x.InnerHtml).Where(imgstring=>imgstring.Contains("img")));
 var allImages = imageColletion.Select(nodeCollection => nodeCollection.Descendants("a"));

HTML:

<div class="entry-thumbnail hover-thumb">
<a href="/" title="Actions for Photographers" rel="bookmark">
<img src="img" class="attachment-thumbnail wp-post-image" alt="Free for Photographers"> </a>
</div>

解决方法 1:

鉴于你的 html,这工作

var imgElems = htmlDoc.DocumentNode
               .SelectNodes("//div[@class='entry-thumbnail hover-thumb']/a/img");

正如标题所说,我没看到任何H元素。

赞助商