网络蜘蛛,收集网页中的邮箱地址信息。如新浪
package tcpudp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SpiderDemo {
public static void main(String[] args) {
try {
URL url = new URL("www.sina.com");
//打开与此url 的连接,并返回一个用于读取该url资源的 InputStream.
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
//正则表达式 邮箱
String regex = "\\w+@\\w+(\\.\\w+)+";
Pattern p = Pattern.compile(regex);
String line = null;
while((br.readLine())!=null){
Matcher m = p.matcher(line);
while(m.find()){//find()对字符串进行匹配,匹配到的字符串可以在任何位置.
/*
* 当使用matches(),lookingAt(),find()执行匹配操作后,就可以利用以下三个方法得到更详细的信息.
* start()返回匹配到的子字符串在字符串中的索引位置.
* end()返回匹配到的子字符串的最后一个字符在字符串中的索引位置.
* group()返回匹配到的子字符串
*/
System.out.println(m.group());
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}