利用HtmlAgilityPack抓取园子里面的数据

   public void GetStr()
        {
            string url = "http://kb.cnblogs.com/list/1001/";
            HtmlWeb web = new HtmlWeb();
            //htmlWeb.DefaultEncoding = System.Text.Encoding.GetEncoding("");

            HtmlAgilityPack.HtmlDocument doc = web.Load(url);
            HtmlNode rootnode = doc.DocumentNode;
            string xpathstring = "//div[@class='list_title']";
            string newpath = "//div[@class='list_title']/a";
            HtmlNodeCollection aa = rootnode.SelectNodes(xpathstring);    //所有找到的节点都是一个集合
            HtmlNodeCollection tt = rootnode.SelectNodes(newpath);
            foreach(HtmlNode ht in tt)
            {
                 string innertext = ht.InnerText;
                string page = ht.GetAttributeValue("href", "");

                string newurl = "http://kb.cnblogs.com"+page;
                string content = GetContent(newurl);
            }
       
        }
        public string GetContent(string url)
        {
              HtmlWeb web = new HtmlWeb();
            //htmlWeb.DefaultEncoding = System.Text.Encoding.GetEncoding("");
            HtmlAgilityPack.HtmlDocument doc = web.Load(url);
            HtmlNode rootnode = doc.DocumentNode;
            string newpath = "//div[@class='contents_main']";
            HtmlNodeCollection tt = rootnode.SelectNodes(newpath);
            return tt[0].InnerText;
        }

posted @ 2013-05-31 18:02  (二少)在南极  阅读(1364)  评论(0编辑  收藏  举报