简单的Java网页爬虫

基础是Java正则表达式,将正则表达式封装成Pattern对象后使用匹配器,与需要获取的数据相关联

import java.io.*;
import java.net.*;
import java.util.regex.*;

public class WebCrawl {
    public static void main(String[] args)throws Exception{
        webCrawl();
    }
    public static void webCrawl()throws Exception{
        URL u = new URL("http://blog.csdn.net/shenqidemao");
        URLConnection conn = u.openConnection();

        BufferedReader bufIn = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String mailRegex = "\\w+@\\w+(.\\w+)+";
        Pattern p = Pattern.compile(mailRegex);
        String line = null;
        while((line = bufIn.readLine())!=null){
            Matcher m = p.matcher(line);
            while(m.find()){
                System.out.println(m.group());
            }
        }
    }
}

你可能感兴趣的:(java,正则表达式)