从互联网上爬邮箱

网页爬虫:其实就是一个程序用于在互联网中获取符合指定规则的数据。

package day05;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SpiderDemo {
	public static void main(String[] args) throws IOException {
		List list = getMailByWeb();
		for (String mail : list) {
			System.out.println(mail);
		}
	}
	
	public static List getMailByWeb() throws IOException {
		URL url = new URL("http://www.itheima.com/aboutt/1376.html");
		BufferedReader input = new BufferedReader(new InputStreamReader(url.openStream()));
		
		String regex = "\\w+@\\w+(\\.\\w+)+";
		Pattern p = Pattern.compile(regex);
		List list = new ArrayList();
		String line = null;
		
		while ((line = input.readLine()) != null) {
			Matcher m = p.matcher(line);
			while (m.find()) {
				list.add(m.group());
			}
		}
		
		return list;
	}
}

你可能感兴趣的:(Java学习)