HtmlAgilityPack-从表中选择td属性

时间:2019-02-27 18:16:01

标签: c# asp.net-mvc html-agility-pack

我正在尝试从此site获取作者姓名。该网站仅显示了25行的结果。每行包含不同的信息,例如作者姓名,标题...等

我尝试了很多解决方案来为每个tr选择作者姓名。但是未能检索到作者姓名..这是我的代码,如果有人可以帮助我知道我错过了什么!

var documentx = new HtmlWeb().Load(post.ExtLink);
        var div = documentx.DocumentNode.SelectNodes("//*//table[2]//tr");
        if (div != null)
        {
            foreach (var item in div)
            {
                Book model = new Book();
                var author= item.SelectSingleNode("//td[1]//a").InnerText.ToString();

                //var title = item.SelectNodes("//td").Skip(2).FirstOrDefault().InnerText;
                //var img = item.Descendants("img").Select(a1 => a1.GetAttributeValue("src", null)).FirstOrDefault();

                model.Book_Description = author;

            }
        }

我想获得这张照片每一行的作者姓名,准确说明我想要的内容:

enter image description here

我尝试调试代码..并且在foreach之前它运行良好,它显示出结果为25行,然后在foreach开始执行时未显示预期结果或值。

1 个答案:

答案 0 :(得分:2)

尝试使用:

   var div = documentx.DocumentNode.SelectNodes("//*//table[3]//tr");

代替:

    var div = documentx.DocumentNode.SelectNodes("//*//table[2]//tr");

并像这样使用它:

var author = item.ChildNodes[0].InnerText;
var series = item.ChildNodes[1].InnerText;
var title = item.ChildNodes[2].InnerText;