【问题标题】:Extracting particular node values from a list of nodes using HtmlAgilityPack in C#在 C# 中使用 HtmlAgilityPack 从节点列表中提取特定节点值
【发布时间】:2012-01-05 05:37:29
【问题描述】:

我正在抓取一个页面 www.thenextweb.com

我想提取所有的帖子链接、文章内容、文章图片等

我已经写了这段代码...

string url = TextBox1.Text.ToString();
        var webGet = new HtmlWeb();
        var document = webGet.Load(url);

        var infos = from info in document.DocumentNode.SelectNodes("//div[@class='article-listing']")
                    select new
                    {
                        Contr = info.InnerHtml
                    };

        lvLinks.DataSource = infos;
        lvLinks.DataBind();

这从页面中提取了所有必需的信息......我在主页中使用了这个信息,在 asp.net 页面中使用 listview 控件作为

<li> <%# Eval("Contr") %> </li>

现在我想要的是一种可以提取节点信息的方法 我们在 infos 中包含所有节点,包括链接 url、发布图片文本等。

我想要一种方法,以便我可以将它们存储为 URL[0]、PostContent[0]、PostImage[0]、Date[0] 和 URL[1]、PostContent[1] 等所有这些都包含受尊重的值正在被存储在这些数组字符串中......每个帖子一个接一个......

这就像从infos中的内部节点中一一提取信息。

请推荐一个方法?

【问题讨论】:

    标签: c# asp.net xpath web-crawler html-agility-pack


    【解决方案1】:

    为什么不创建一个解析 HTML 并将这些节点作为属性公开的类。

    class ArticleInfo
    {
        public ArticleInfo (string html) { ... }
        public string URL { get; set; }
        public string PostContent { get; set; }
        public string PostImage { get; set; }
        public DateTime PostDate { get; set; }
    }
    

    然后你可以这样做:

    var infos = from info in document.DocumentNode.SelectNodes("//div[@class='article-listing']")
                select new ArticleInfo(info.InnerHtml);
    

    那么,如果你有一个由这些 `infoArray = infos.ToArray()' 组成的数组,你可以这样做:

    infoArray[0].URL
    infoArray[0].PostDate
    infoArray[1].PostContent
    
    etc...
    

    更新

    类似这样的:

    class ArticleInfo
    {
        private string html;
    
        public ArticleInfo (string html) 
        {
            this.html = html;
            URL = //code to extract and assign Url from html
            PostContent = //code to extract content from html
            PostImage = //code to extract Image from html
            PostDate = //code to extract date from html
        }
    
        public string URL { get; private set; }
        public string PostContent { get; private set; }
        public string PostImage { get; private set; }
        public DateTime PostDate { get; private set; }
    
        public string Contr { get { return html; } }
    }
    

    或者这个:

    class ArticleInfo
    {
        private string html;
    
        public ArticleInfo (string html) 
        {
            this.html = html;
        }
    
        public string URL { get { return /*code to extract and return Url from html*/; } }
        public string PostContent { get { return /*code to extract and return Content from html*/; } }
        public string PostImage { get { return /*code to extract and return Image from html*/; } }
        public DateTime PostDate { get { return /*code to extract and return Date from html*/; } }
    
        public string Contr { get { return html; } }
    }
    

    然后您的链接查询将返回ArticleInfo 序列,而不是匿名类型。这样您就不必为帖子的每个元素维护单独的数组。数组(或序列)中的每个项目都有属性,可以为您提供该项目的关联元素。当然,这可能不适合您想要实现的目标。我只是觉得它可能会更干净一些。

    【讨论】:

    • 嘿,我没明白你的意思...你能给我提供完整的代码来访问我在问题中所说的信息吗?
    猜你喜欢
    • 2018-05-03
    • 2014-08-06
    • 2018-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    相关资源
    最近更新 更多