如何在c#中使用htmlagilitypack访问子节点【基于'from'条款]

发布于 2024-12-25 06:10:50 字数 2463 浏览 0 评论 0原文

我有一个 html :

    <div class="article-listing">
        <div class="media-data">

                             <h4><a href="http://thenextweb.com/media/2012/01/05/24symbols-white-labels-its-platform-to-give-any-organization-its-own-spotify-for-books/">24symbols White-Labels its 'Spotify for Books'</a></h4>
                             <p class="article-meta"><a href="http://thenextweb.com/media/">TNW Media</a> &#8226; <a href="http://thenextweb.com/author/martin/" title="Posts by Martin Bryant" rel="author">Martin Bryant</a>  &#8226; <span class="date" title="1325781355">January 5, 2012</span></a></p>

                             <p>24symbols, the &#8216;Spotify for books&#8217; startup that launched last summer, has been busy developing its service that allows users instant access to a library of books for a fixed fee&#8230;.</p>
                         </div></div>

I am using this xpath code for extracting required information :

    var webGet = new HtmlWeb();
                var document = webGet.Load(page);

                var infos = from info in document.DocumentNode.SelectNodes("//div[@class='article-listing']//div[@class='media-data']")
                            from link in info.Descendants("a").Where(x => x.Attributes.Contains("href"))
                            from content in info.Descendants("p").Where(y => y.HasAttributes != true)
                            from author in info.Descendants("//p[@class='article-meta']//a[@rel='author']").Where(z => z.Attributes.Contains("href"))
                            from date in info.Descendants("//p[@class='article-meta']//span")
                            select new
                                {
                                   LinkURL = link.Attributes["href"].Value,
                                   Text = content.InnerText,
                                   Author = author.InnerText,
                                   Date = date.InnerText
                                };
    lvLinks.DataSource = infos;
                lvLinks.DataBind();   

我正在使用列表视图控件在 asp 页面中显示数据,如使用

  • ; <%# Eval("LinkURL") %> - <%# Eval("文本") %> - <%# Eval("作者") %>
  • 但它不起作用...它没有显示任何错误,也没有在页面上显示任何数据。

    使用不同的“from”子句选择节点可能存在一些问题。

    请推荐我 谢谢

    i have a html as :

        <div class="article-listing">
            <div class="media-data">
    
                                 <h4><a href="http://thenextweb.com/media/2012/01/05/24symbols-white-labels-its-platform-to-give-any-organization-its-own-spotify-for-books/">24symbols White-Labels its 'Spotify for Books'</a></h4>
                                 <p class="article-meta"><a href="http://thenextweb.com/media/">TNW Media</a> • <a href="http://thenextweb.com/author/martin/" title="Posts by Martin Bryant" rel="author">Martin Bryant</a>  • <span class="date" title="1325781355">January 5, 2012</span></a></p>
    
                                 <p>24symbols, the ‘Spotify for books’ startup that launched last summer, has been busy developing its service that allows users instant access to a library of books for a fixed fee….</p>
                             </div></div>
    
    I am using this xpath code for extracting required information :
    
        var webGet = new HtmlWeb();
                    var document = webGet.Load(page);
    
                    var infos = from info in document.DocumentNode.SelectNodes("//div[@class='article-listing']//div[@class='media-data']")
                                from link in info.Descendants("a").Where(x => x.Attributes.Contains("href"))
                                from content in info.Descendants("p").Where(y => y.HasAttributes != true)
                                from author in info.Descendants("//p[@class='article-meta']//a[@rel='author']").Where(z => z.Attributes.Contains("href"))
                                from date in info.Descendants("//p[@class='article-meta']//span")
                                select new
                                    {
                                       LinkURL = link.Attributes["href"].Value,
                                       Text = content.InnerText,
                                       Author = author.InnerText,
                                       Date = date.InnerText
                                    };
        lvLinks.DataSource = infos;
                    lvLinks.DataBind();   
    

    I am using list view control to show data in asp page as using <li> <%# Eval("LinkURL") %> - <%# Eval("Text") %> - <%# Eval("Author") %> </li>

    But its not working...It is not showing any errors and nor it shows any data on the page..

    May be there is some problem with selecting nodes using different 'from' clauses.

    Please suggest me
    Thanks

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(1

    温柔戏命师 2025-01-01 06:10:50

    我已经解决了这个问题......这是与节点的选择有关。需要使用 SelectNodes 而不是后代,我还需要修复 SelectNodes() 方法中传递的标签。
    我们可以使用下面的代码,它会工作得很好。

     var infos = from info in document.DocumentNode.SelectNodes("//div[@class='article-listing']//div[@class='media-data']")
        from link in info.SelectNodes("h4//a").Where(x => x.Attributes.Contains("href"))
        from content in info.SelectNodes("p").Where(y => y.HasAttributes != true)
        from author in info.SelectNodes("p[@class='article-meta']//a[@rel='author']").Where(z => z.Attributes.Contains("href"))
        from date in info.SelectNodes("p[@class='article-meta']//span")
    
        select new
         {
           LinkURL = link.Attributes["href"].Value,
           Text = content.InnerText,
           Author = author.InnerText,
           Date = date.InnerText
         };
    

    lvLinks.DataSource = 信息;
    lvLinks.DataBind();

    工作正常...现在没问题

    I have resolved the issue... It was with selection of nodes. One need to use SelectNodes instead of Descendants and also i need to fix the tags passed in SelectNodes() method.
    We can use following code and it would work fine.

     var infos = from info in document.DocumentNode.SelectNodes("//div[@class='article-listing']//div[@class='media-data']")
        from link in info.SelectNodes("h4//a").Where(x => x.Attributes.Contains("href"))
        from content in info.SelectNodes("p").Where(y => y.HasAttributes != true)
        from author in info.SelectNodes("p[@class='article-meta']//a[@rel='author']").Where(z => z.Attributes.Contains("href"))
        from date in info.SelectNodes("p[@class='article-meta']//span")
    
        select new
         {
           LinkURL = link.Attributes["href"].Value,
           Text = content.InnerText,
           Author = author.InnerText,
           Date = date.InnerText
         };
    

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

    Its working fine...No issue now

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文