zoukankan      html  css  js  c++  java
  • 用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#开发蜘蛛网络爬虫采集程序(二)

     源代码下载

    量的积累到质的飞越

  • 相关阅读:
    [HBase] HBase 安装&&小测试
    [tcl] hello tcl
    [php] 数据结构&算法(PHP描述) 快速排序 quick sort
    [php] 数据结构&算法(PHP描述) 查找&&二分法查找
    Linux 系统信息监控统计命令小结
    fuser 命令小结
    hdu 1142 A Walk Through the Forest (最短路+dfs)
    hdu 1688 Sightseeing (次短路及次短路数)&&pku3255 Roadblocks &&3191 How Many Paths Are There
    hdu 3790 最短路径问题
    hdu 2415 Bribing FIPA(树形DP)
  • 原文地址:https://www.cnblogs.com/taobox/p/2691705.html
Copyright © 2011-2022 走看看