爬取本地html中符合正则规范的数据。 要爬取网络 需更换流地址以及正则。
package Socket;
import com.sun.deploy.net.MessageHeader;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class 本地爬虫 {
public static void main(String[] args) throws IOException {
List<String> list = getMails();
for(String mail:list){
System.out.println(mail);
}
}
public static List<String> getMails() throws IOException {
BufferedReader bufferedReader = new BufferedReader(new FileReader("D:\\mails.html"));
String mail_regex = "\\w+@\\w+(\\.\\w{3})+";
Pattern pattern = Pattern.compile(mail_regex);
String line = null;
List<String> list = new ArrayList<String>();
while ((line = bufferedReader.readLine())!=null){
Matcher m = pattern.matcher(line);
while (m.find()){
list.add(m.group());
}
}
bufferedReader.close();
return list;
}
}