HtmlAgilityPack 学习和笔记

介绍:

http://www.cnblogs.com/bomo/archive/2013/01/28/2879361.html 

实战 c#获取外网ip

网址:http://ip138.com/

如图:分析

 

有个iframe 实际上查看网页源代码是没有ip的 原因浏览器引擎解析会解析src 地址:

所以我们先获取iframe的url 在根据这个地址 取ip (当然也有人说直接用这个url 不好吗?这里考虑的是怕他变化 容错性好一点 将来)

 

代码:

        static void Main(string[] args)
        {
            string content = GetHtmlStr("http://www.ip138.com/", Encoding.GetEncoding("gb2312"));
            var ulr = "";
            HtmlDocument document = new HtmlDocument();
            document.LoadHtml(content);

            HtmlNodeCollection targetNodeCollection = document.DocumentNode.SelectNodes(@"//iframe [1]");//取iframe DOM 第一个
            if (targetNodeCollection != null && targetNodeCollection.Count == 1)
            {
                ulr = targetNodeCollection[0].GetAttributeValue("src", "");//类似jq $(xxx).attr("scr") 对应javascript getattribute
            }
            content = GetHtmlStr(ulr, Encoding.GetEncoding("gb2312"));
            document.LoadHtml(content);
            HtmlNodeCollection tempText = document.DocumentNode.SelectNodes(@"//center [1]");//取center DOM 第一个
            Console.WriteLine(tempText[0].InnerText);//这里就没有过滤了
        }

        public static string GetHtmlStr(string url, Encoding en)
        {
            try
            {
                WebRequest rGet = WebRequest.Create(url);
                rGet.Timeout = 30000;
                using (WebResponse rSet = rGet.GetResponse())
                {
                    using (StreamReader reader = new StreamReader(rSet.GetResponseStream(), en))
                    {
                        return reader.ReadToEnd();
                    }
                }
            }
            catch (WebException)
            {
                //连接失败
                return null;
            }
        }

结果:

 

 

            string content = GetHtmlStr("xxxx", Encoding.GetEncoding("gb2312"));
            HtmlDocument document = new HtmlDocument();
            document.LoadHtml(content);

            HtmlNodeCollection targetNodeCollection = document.DocumentNode.SelectNodes(@"//font[@class='small2']");

            Console.WriteLine(targetNodeCollection[targetNodeCollection.Count-1].InnerText);

            HtmlNodeCollection str = document.DocumentNode.SelectNodes("//td[@class='small']//table[1]//tbody[1]//tr[1]//td[2]");
            Console.WriteLine(str[0].InnerText);

            string xpathstring = "//td[@class='small']";
            HtmlNodeCollection aa = document.DocumentNode.SelectNodes(xpathstring);    //所有找到的节点都是一个集合
            foreach (var htmlNode in aa.Where(htmlNode => htmlNode.GetAttributeValue("style", " ") == "word-wrap:break-word;width:100%;left:0;word-break :break-all;margin-right:1px;"))
            {
                Console.WriteLine(htmlNode.ChildNodes[0].InnerText.Replace("\n", string.Empty).Replace("\t", string.Empty));
                break;
            }
            Console.ReadKey();

 

posted @ 2014-07-23 16:24  s_p  阅读(260)  评论(0编辑  收藏  举报