zoukankan      html  css  js  c++  java
  • 自己写的一个抓取页面email地址的小程序

    媳妇的工作需要群发邮件,见她一个一个的复制粘贴,有些不忍,编写了一个邮箱批量抓取的小程序,对她的工作有些小帮助。

    抓取页面的emal地址,只需要两个步骤,1,通过url地址抓取页面信息,2,从页面信息中提取emal地址。下面奉上代码;

    public static string GetHtml(string url)
            {
                string html = "";
                try
                {
                    WebClient MyWebClient = new WebClient();
    
                    Encoding utf8 = Encoding.UTF8;
    
                    Encoding defaultCode = Encoding.Default;
    
                    MyWebClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于对向Internet资源的请求进行身份验证的网络凭据。
    
                    Byte[] pageData = MyWebClient.DownloadData(url);//从指定网站下载数据
                    string pageHtml = Encoding.Default.GetString(pageData);
                    string Encodeing = getEncoding(pageHtml);
                    if (Encodeing != "GB2312" && Encodeing != "GBK")
                    {
                        byte[] defaultBytes = Encoding.Convert(utf8, defaultCode, pageData);
                        pageHtml = Encoding.Default.GetString(defaultBytes);  //如果获取网站页面采用的是GB2312,则使用这句  
                    }
    
                    html = pageHtml;
                }
                catch (WebException webEx)
                {
                    Console.WriteLine(webEx.Message.ToString());
                }
                return html;
            }
            public static string getEncoding(string html)
            {
                Match match = Regex.Match(html, "charset=(?'ecncoding'.*[\\d|k|K])\"");
                GroupCollection groups = match.Groups;
                string ecncoding = groups["ecncoding"].Value;
                return ecncoding.ToUpper();
            }
    
            protected void btnOk_Click(object sender, EventArgs e)
            {
                lstEmails.Items.Clear();
                string html = GetHtml(this.txturl.Text.Trim());
                string reg = @"[a-zA-Z0-9_\-\.]+@\w+(\.\w+)+";//匹配Email的正则表达式
                MatchCollection matchs = Regex.Matches(html, reg);foreach (Match ms in matchs)
                {
                    txtEmails.Items.Add(new ListItem(ms.Value+",\r\n"));
                }
            }
    GetHtml()方法用来抓取页面信息。getEncoding()获取页面编码
  • 相关阅读:
    2013面试C++小结
    Linux C 面试题总结 .
    [SCOI2011]糖果
    python——简单爬虫
    python——ADSL拨号程序
    python——处理xls表格
    Vsphere初试——架设Panabit行为管理
    Vsphere初试——使用Vsphere client
    Vsphere初试——基本安装
    Python2与Python3的不同点
  • 原文地址:https://www.cnblogs.com/zlzly/p/2864731.html
Copyright © 2011-2022 走看看