我們可以先下載一個網頁檔案在自己的硬碟上,然後通過URL地址讀取檔案,利用這則運算式一一對比所讀出的字串是否是email地址,實現抓取!傳說的垃圾郵件首先就是要先再網頁中抓取別人的email地址,具體的小程式如下:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Pattern;
import java.util.regex.*;
public class Spider {
public static void main(String[] args) {
try {
BufferedReader buf = new BufferedReader(new FileReader("F:\\a.mht"));
String line = "";
while ((line = buf.readLine()) != null)
pattern(line);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void pattern(String line) {
Pattern p = Pattern.compile("[\\w[._]]+@[\\w[._]]+\\.[\\w]+");//email地址的Regex;
Matcher m = p.matcher(line);
while(m.find()){
System.out.println(m.group());
}
/*while(p.matcher(line)!=null){
}*/
}
}