网络爬虫范例

网络爬虫实例
package com.cgm.regx;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NetSpider {

/**
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) throws Exception {
String str_url = "http://mail.qq.com/cgi-bin/frame_html?sid=Hqh55ia3DozCEjBj&r=639d2064091e9b578fde1576e5b88b9f";
URL url = new URL(str_url);
URLConnection ucon = url.openConnection();
BufferedReader bfr = new BufferedReader(new InputStreamReader(ucon.getInputStream()));
// BufferedReader bfr = new BufferedReader(new FileReader("F:/spidertest/index.jsp"));
String line = null;
while ((line = bfr.readLine()) != null) {
System.out.println(line);
String res = "\\w+@\\w+(\\.\\w+)+";
Pattern p = Pattern.compile(res);
Matcher m = p.matcher(line);
while (m.find()) {
System.out.println(m.group());
}

}
bfr.close();

}

}