zoukankan      html  css  js  c++  java
  • 抓取网页Email地址

     public class GetWebEmail
        {
            //抓取网页源代码
            public static List<string> GetHtmlAndEmail(string url)
            {
                //抓取网页内容
                string ContentHtml = String.Empty;

                HttpWebRequest httpWebRequest = null;
                HttpWebResponse httpWebResponse = null;
                Stream stream = null;
                StreamReader sr = null;

                httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                stream = httpWebResponse.GetResponseStream();
                Encoding encoding = Encoding.Default;
                sr = new StreamReader(stream, encoding);
                ContentHtml = sr.ReadToEnd();

                //将读取出来的全部URL写入文本文件 
                string fileName = HttpContext.Current.Server.MapPath(@"~/temp/EmailText.txt");//创建文本文档
                StreamWriter sw = File.AppendText(fileName);//创建写入流,这里是以追加的模式就行的


                //用正则表达式识别Email地址
                Regex EmailRegex = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                MatchCollection matches = EmailRegex.Matches(ContentHtml);
                List<string> list = new List<string>();
                foreach (Match match in matches)
                {
                    list.Add(match.Value.ToString());  //将数据添加到list
                    sw.WriteLine(match.Value.ToString());//将数据写入文件
                }
               

                sw.Close();
                sr.Close();
                stream.Close();
                httpWebResponse.Close();
                return list;
            }
        }

    //今天突然想起来实现抓取网页中的Email地址,这样可以去收集网络中的地址去打些广告不过这不是我的初衷,我只是突发奇想,想实现这个功能罢 了,以上类是我实现的抓取网页Email地址的方法,不过还没有将发送邮件的程序和该程序做练习,以前写过发送邮件的程序,有兴趣可以在我空间的日志中取 查找!有不对的地方请高手支出,共同提高……

  • 相关阅读:
    一图看懂host_only nat bridge拓扑结构
    Linux iptables原理--数据包流向
    linux iptables常用命令之配置生产环境iptables及优化
    linux service命令解析
    mysql 二进制日志后缀数字最大为多少
    mysql基于init-connect+binlog完成审计功能
    MySQL5.7(5.6)GTID环境下恢复从库思(qi)路(yin)方(ji)法(qiao)
    Centos6.5 python升级成2.7版本出现的一些问题解决方法
    python生成随机密码
    (原创)c++中的类型擦除
  • 原文地址:https://www.cnblogs.com/wangsx/p/2039186.html
Copyright © 2011-2022 走看看