java爬虫--使用正则表达式获取网页中的email

时间:2023-03-09 01:17:50
java爬虫--使用正则表达式获取网页中的email
package com.enation.newtest;
import java.io.*;
import java.util.regex.*;
import java.net.*; public class MailTest{
public static void main(String[] args) throws Exception{
getMailAddr();
} public static void getMailAddr()throws Exception{
URL url=new URL("http://blog.sina.com.cn/s/blog_515617e60101e151.html");
URLConnection con=url.openConnection(); BufferedReader bufIn=new BufferedReader(new InputStreamReader(con.getInputStream()));
BufferedWriter bufw=new BufferedWriter(new FileWriter(new File("D:\\360Downloads\\mailaddress2.txt")));
String str=null;
String regex="[a-zA-Z0-9_]{6,12}@[a-zA-Z0-9]+(.[a-zA-Z]+)+"; Pattern p=Pattern.compile(regex);
System.out.println("start");
while((str=bufIn.readLine())!=null) {
Matcher m=p.matcher(str);
while(m.find()){
String ss=m.group();
bufw.write(ss,0,ss.length());
bufw.newLine();
bufw.flush();
}
}
System.out.println("end");
}
}

获取网页内容,并将页面中的邮件地址存存放在指定的路径中,写入到txt文件里