• 用C#开发蜘蛛网络爬虫采集程序(附源码)(二)


    上次已经可以得到页面的HTML代码了,接下来需要对HTML代码分析,得到里面所有的链接和过滤掉没用的HTML代码,把文字内容保留下来。

    分析HTML代码,通过正规表达式将链接和链接的文字内容保存下来。

        private void FindLink(string html)
        {
            this.TextBox3.Text="";
            List<string> hrefList = new List<string>();//链接
            List<string> nameList = new List<string>();//链接名称
    
            string pattern = @"<a\s*href=(""|')(?<href>[\s\S.]*?)(""|').*?>\s*(?<name>[\s\S.]*?)</a>";
            MatchCollection mc = Regex.Matches(html, pattern);
            foreach (Match m in mc)
            {
                if (m.Success)
                {
                    //加入集合数组
                    hrefList.Add(m.Groups["href"].Value);
                    nameList.Add(m.Groups["name"].Value);
                    this.TextBox3.Text += m.Groups["href"].Value + "|" + m.Groups["name"].Value + "\n";
                }
            }
        }

    这个方法只实现简单的找到链接,并没有过滤掉#或javascript:void(0)这样的内容。

    接下要过滤掉没有用的HTML代码,保留文字内容,基本还是正规表达式,网上还有很多种方法,写的正规的HTML页面都可以正常过滤掉,不过对于那些代码都不成对的、不按常理出牌的网站,我就很无语了……

        public string ClearHtml(string text)//过滤html,js,css代码
        {
            text = text.Trim();
            if (string.IsNullOrEmpty(text))
                return string.Empty;
            text = Regex.Replace(text, "<head[^>]*>(?:.|[\r\n])*?</head>", "");
            text = Regex.Replace(text, "<script[^>]*>(?:.|[\r\n])*?</script>", "");
            text = Regex.Replace(text, "<style[^>]*>(?:.|[\r\n])*?</style>", "");
            
            text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", ""); //<br> 
            text = Regex.Replace(text, "\\&[a-zA-Z]{1,10};", "");
            text = Regex.Replace(text, "<[^>]*>", "");
    
            text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", ""); //&nbsp;
            text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); //其它任何标记
            text = Regex.Replace(text, "[\\s]{2,}", " "); //两个或多个空格替换为一个
    
            text = text.Replace("'", "''");
            text = text.Replace("\r\n", "");
            text = text.Replace("  ", "");
            text = text.Replace("\t", "");
            return text.Trim();
        }

    最后再加个通过URL分析IP地址的方法,有些域名做均衡负载的都可以分析出多个IP,不过只能在本地运行,放IIS上需要完整的信任级别,关于信任级别的说明请点这里

        private void IPAddresses(string url)
        {
            url = url.Substring(url.IndexOf("//") + 2);
            if (url.IndexOf("/") != -1)
            {
                url = url.Remove(url.IndexOf("/"));
            }
            this.Literal1.Text += "<br>" + url;
            try
            {
                System.Text.ASCIIEncoding ASCII = new System.Text.ASCIIEncoding();
                IPHostEntry ipHostEntry = Dns.GetHostEntry(url);
                System.Net.IPAddress[] ipaddress = ipHostEntry.AddressList;
                foreach (IPAddress item in ipaddress)
                {
                    this.Literal1.Text += "<br>IP:" + item;
                }
            }
            catch { }
        }

     用C#开发蜘蛛网络爬虫采集程序(一)

     用C#开发蜘蛛网络爬虫采集程序(二)

     源代码下载

    量的积累到质的飞越

  • 相关阅读:
    nginx 配置https 负载均衡
    MyCAT+MySQL搭建高可用企业级数据库集群视频课程
    Java数字签名算法--RSA
    bootstrap在iframe框架中实现由子页面在顶级页面打开模态框(modal)
    bootstrap-treeview 自定义实现双击事件
    Java多线程之内存可见性
    Java实现责任链模式
    JVM(HotSpot) 7种垃圾收集器的特点及使用场景
    jQuery的noConflict以及插件扩展
    JavaScript事件漫谈
  • 原文地址:https://www.cnblogs.com/taobox/p/2691705.html
Copyright © 2020-2023  润新知