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();

 

转载于:https://www.cnblogs.com/y112102/p/3863322.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值