java程序--从网页中提取电子邮箱地址

来源:互联网 发布:java开发实例 编辑:程序博客网 时间:2024/05/22 13:06

先读取网页html代码,然后通过正则表达式提取点邮箱

public static void main(String[] args){

    URL url;

   try {

      url = new URL("http://home.focus.cn/msgview/607/1/9858619.html"); //网页url

     HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection(); //连接

     urlConnection.connect();

     InputStream in = urlConnection.getInputStream();   //获取输入流

     byte[] data = new byte[4096];            

    while (in.read(data) > 0) {         //读取流文件

      String tempString = new String(data);

      String regex2 = "[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+";

       Matcher m=Pattern.compile(regex2).matcher(tempString);  

          while(m.find()) {

              System.out.println(m.group());

          }

      }

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}