当前位置: 首页>>代码示例>>C#>>正文


C# HtmlDocument.LoadUri方法代码示例

本文整理汇总了C#中HtmlAgilityPack.HtmlDocument.LoadUri方法的典型用法代码示例。如果您正苦于以下问题:C# HtmlDocument.LoadUri方法的具体用法?C# HtmlDocument.LoadUri怎么用?C# HtmlDocument.LoadUri使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在HtmlAgilityPack.HtmlDocument的用法示例。


在下文中一共展示了HtmlDocument.LoadUri方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetCategories

        public List<Objects.Section> GetCategories(string sitename)
        {
            List<Objects.Section> sections = new List<Objects.Section>();

            HtmlAgilityPack.HtmlDocument page = new HtmlAgilityPack.HtmlDocument();
            page.LoadUri("http://" + sitename + ".craigslist.org/");
            foreach (HtmlNode tempSection in page.DocumentNode.SelectNodes("//h4").Where(x => x.InnerText != "" && x.Attributes.Where(y => y.Name == "class").Count() > 0 && x.Attributes["class"].Value.ToString() == "ban" && x.InnerText != "discussion forums"))
            {
                Objects.Section s = new Objects.Section();
                s.Name = tempSection.InnerText;

                foreach (HtmlNode tempCategory in tempSection.NextSibling.NextSibling.DescendantNodes().Where(x => x.Name == "li"))
                {
                    Objects.Category c = new Objects.Category();
                    c.Name = tempCategory.InnerText;
                    string v = tempCategory.FirstChild.Attributes["href"].Value.ToString();

                    if (v.Length >= 9 && v.Substring(0, 9) == "/cgi-bin/")
                        v = v.Substring(v.Length - 4);

                    c.Value = v;
                    s.Categories.Add(c);
                }

                sections.Add(s);
            }

            return sections;
        }
开发者ID:rera,项目名称:code-samples,代码行数:29,代码来源:DS.svc.cs

示例2: GetPostDetail

        public Objects.Post GetPostDetail(string link)
        {
            Objects.Post p = new Objects.Post();
            HtmlAgilityPack.HtmlDocument detailpage = new HtmlAgilityPack.HtmlDocument();
            detailpage.LoadUri(link);

            int startLine = detailpage.DocumentNode.SelectNodes("//h2").First().Line;
            int endLine = detailpage.DocumentNode.ChildNodes.Where(x => x.NodeType == HtmlNodeType.Comment && x.InnerHtml.Contains("END CLTAGS")).First().Line;

            StringBuilder sb = new StringBuilder();
            foreach (HtmlNode c in detailpage.DocumentNode.ChildNodes.Where(x => x.Line >= startLine && x.Line <= endLine))
                sb.Append(c.OuterHtml);
            p.Content = sb.ToString();

            if (detailpage.DocumentNode.SelectNodes("//table").Where(x => x.Attributes.Where(y => y.Name == "summary").Count() > 0 && x.Attributes["summary"].Value.ToString() == "craigslist hosted images").Count() > 0)
            {
                foreach (HtmlNode n in detailpage.DocumentNode.SelectNodes("//table").Where(x => x.Attributes.Where(y => y.Name == "summary").Count() > 0 && x.Attributes["summary"].Value.ToString() == "craigslist hosted images").First().DescendantNodes().Where(x => x.Name == "img"))
                {
                    Objects.PostImage pi = new Objects.PostImage();
                    pi.Link = n.Attributes["src"].Value.ToString();
                    p.PostImages.Add(pi);
                }
            }

            return p;
        }
开发者ID:rera,项目名称:code-samples,代码行数:26,代码来源:DS.svc.cs

示例3: GetPosts

        public List<Objects.Post> GetPosts(string sitename, string category)
        {
            List<Objects.Post> posts = new List<Objects.Post>();

            HtmlAgilityPack.HtmlDocument page = new HtmlAgilityPack.HtmlDocument();
            page.LoadUri("http://" + sitename + ".craigslist.org/" + category);
            foreach (HtmlNode tempPost in page.DocumentNode.SelectNodes("//p"))
            {
                if (tempPost.DescendantNodes().Where(x => x.Name == "a").Count() > 0)
                {
                    Objects.Post p = new Objects.Post();

                    HtmlNode a = tempPost.DescendantNodes().Where(x => x.Name == "a").First();
                    p.Title = a.InnerText;
                    p.Link = a.Attributes["href"].Value.ToString();

                    string price = tempPost.InnerText.Split('-')[1];
                    if (price.Contains('$'))
                        p.Price = price.Split('(')[0];
                       // p.Price = Regex.Replace(price, "[^.0-9]", "");

                    if (tempPost.DescendantNodes().Where(x => x.Name == "font").Count() > 0)
                    {
                        string l = tempPost.DescendantNodes().Where(x => x.Name == "font").First().InnerText;
                        l = l.Substring(2, l.Length - 3);
                        if (l != "")
                            p.Location = l;
                    }

                    DateTime d = DateTime.Today;
                    if (page.DocumentNode.SelectNodes("//h4").Where(x => x.Attributes.Where(y => y.Name == "class").Count() > 0 && x.Attributes["class"].Value.ToString() == "ban" && x.Line <= tempPost.Line).Count() > 0)
                    {
                        string t1 = page.DocumentNode.SelectNodes("//h4").Where(x => x.Attributes.Where(y => y.Name == "class").Count() > 0 && x.Attributes["class"].Value.ToString() == "ban" && x.Line <= tempPost.Line).Last().InnerText;
                        d = DateTime.Parse(t1.Split(' ')[1] + "/" + t1.Split(' ')[2] + "/" + DateTime.Today.Year.ToString());
                    }

                    p.Date = d;

                    if (tempPost.DescendantNodes().Where(x => x.Name == "span" && x.Attributes.Where(y => y.Name == "class").Count() > 0 && x.Attributes["class"].Value.ToString() == "p" && (x.InnerText == "pic" || x.InnerText == "img")).Count() > 0)
                    {
                        HtmlAgilityPack.HtmlDocument detailpage = new HtmlAgilityPack.HtmlDocument();
                        detailpage.LoadUri(p.Link);

                        int startLine = detailpage.DocumentNode.SelectNodes("//h2").First().Line;
                        int endLine = detailpage.DocumentNode.ChildNodes.Where(x => x.NodeType == HtmlNodeType.Comment && x.InnerHtml.Contains("END CLTAGS")).First().Line;

                        StringBuilder sb = new StringBuilder();
                        foreach (HtmlNode c in detailpage.DocumentNode.ChildNodes.Where(x => x.Line >= startLine && x.Line <= endLine))
                            sb.Append(c.OuterHtml);
                        p.Content = sb.ToString();

                        if (detailpage.DocumentNode.SelectNodes("//table").Where(x => x.Attributes.Where(y => y.Name == "summary").Count() > 0 && x.Attributes["summary"].Value.ToString() == "craigslist hosted images").Count() > 0)
                        {
                            foreach (HtmlNode n in detailpage.DocumentNode.SelectNodes("//table").Where(x => x.Attributes.Where(y => y.Name == "summary").Count() > 0 && x.Attributes["summary"].Value.ToString() == "craigslist hosted images").First().DescendantNodes().Where(x => x.Name == "img"))
                            {
                                Objects.PostImage pi = new Objects.PostImage();
                                pi.Link = n.Attributes["src"].Value.ToString();
                                p.PostImages.Add(pi);
                            }
                        }
                    }

                    posts.Add(p);
                }
            }

            return posts;
        }
开发者ID:rera,项目名称:code-samples,代码行数:68,代码来源:DS.svc.cs

示例4: GetSites

        public List<Objects.Country> GetSites()
        {
            List<Objects.Country> countries = new List<Objects.Country>();

            HtmlAgilityPack.HtmlDocument page = new HtmlAgilityPack.HtmlDocument();
            page.LoadUri("http://www.craigslist.org/about/sites");
            foreach(HtmlNode country in page.DocumentNode.SelectNodes("//h1").Where(x => x.InnerText != ""))
            {
                Objects.Country c = new Objects.Country();
                c.Name = country.InnerText;

                foreach (HtmlNode state in country.NextSibling.NextSibling.DescendantNodes().Where(x => x.Name == "div" && x.Attributes.Where(y => y.Name == "class").Count() > 0 && x.Attributes["class"].Value.ToString() == "state_delimiter" && x.InnerText != ""))
                {
                    Objects.State s = new Objects.State();
                    s.Name = state.InnerText;

                    foreach (HtmlNode site in state.NextSibling.NextSibling.ChildNodes.Where(x => x.Name == "li"))
                    {
                        Objects.Site si = new Objects.Site();
                        si.Name = site.FirstChild.InnerText;
                        si.Value = site.FirstChild.Attributes["href"].Value.ToString().Split('.').First().Substring(7);
                        s.Sites.Add(si);
                    }
                    c.States.Add(s);
                }
                countries.Add(c);
            }

            return countries;
        }
开发者ID:rera,项目名称:code-samples,代码行数:30,代码来源:DS.svc.cs


注:本文中的HtmlAgilityPack.HtmlDocument.LoadUri方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。