HtmlAgilityPack从div的列表中选择单个元素 [英] HtmlAgilityPack Select individual elements from a list of divs

查看:626
本文介绍了HtmlAgilityPack从div的列表中选择单个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从使用div的列表中HtmlAgilityPack子元素凑。最母公司事业部为 // DIV [@类='细胞在面积小区中间单元'] 如果我只是遍历列表我可以显示每个父精所有子内容

I am trying to scrape using the HtmlAgilityPack child elements from a list of divs. The most parent Div is //div[@class='cell in-area-cell middle-cell'] and if I simply iterate through the list I can display all the child content from each parent fine.

但我不希望显示的所有内容,我想挑选几格的,对的和的每个孩子,但低于code仅给我的第一个列表的 // a [@类='挂牌名称'] 。它给我lstRecords正确的数量,但它们都具有相同的值。

But I don't want to display all the content, I would like to pick certain div's, p's and a's from each of the children but the code below is only giving me a list of the first //a[@class='listing-name']. It gives me the correct number of lstRecords but they all have the same value.

下面是我的code:

型号:

public class TempSearch
{
    public string listing_name { get; set; }
}

查看:

@model List<tempsearch.Models.TempSearch>

@foreach (var ps in Model)
{
    <h4>@Html.Raw(ps.listing_name)</h4>
}

控制:

public ActionResult TempSearch()
{
    string html = Server.MapPath("~/Content/tempsearch.html");

    HtmlWeb web = new HtmlWeb();
    HtmlDocument document = web.Load(html);

    List<TempSearch> lstRecords = new List<TempSearch>();

    foreach (HtmlNode node in document.DocumentNode.SelectNodes("//div[@class='cell in-area-cell middle-cell']"))
    {
        TempSearch tempSearch = new TempSearch();

        HtmlNode node2 = document.DocumentNode.SelectSingleNode("//a[@class='listing-name']");

        tempSearch.listing_name += node2.InnerHtml.Trim();
        lstRecords.Add(tempSearch);

    }
    return View(lstRecords);
}

我猜它是与我正在填充列表的方式吗?

I guess it has something to do with the way i'm populating the list?

推荐答案

您想使用相对于当前由引用的元素节点变量,像这样的XPath:

You want to use XPath relative to element currently referenced bynode variable, like this :

HtmlNode node2 = node.SelectSingleNode(".//a[@class='listing-name']");

注意在的XPath的开头这表明XPath是相对的当前上下文元素的SelectSingleNode()要求节点变量,使节点当前上下文元素的。否则,您总能获得相同的元素一遍又一遍地在每次迭代。

Notice the . at the beginning of the XPath which indicate that the XPath is relative to current context element, and SelectSingleNode() method called on node variable to make node as the current context element. Otherwise, you'll always get the same element over and over on each iteration.

这篇关于HtmlAgilityPack从div的列表中选择单个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆