网页爬虫
importjava.net.*; importjava.io.*; importjava.util.regex.*; classfindMail { publicstaticvoidmain(String[]args)throwsException { //读取流关联文件 //BufferedReaderbin=newBufferedReader(newFileReader("mail.txt")); //获取网页上的数据需要获取输入流来自网页端的URLConnection的getInputStream()来获取输入流 URLurl=newURL("http://127.0.0.1:8080/myweb/mail.html"); URLConnectionconn=url.openConnection(); BufferedReaderbin=newBufferedReader(newInputStreamReader(conn.getInputStream())); Stringline=null; //定义邮箱格式正则规则 Stringmailreg="\\w{2,13}@\\w{2,5}(\\.[a-z]+)+"; //正则规则封装模式对象 Patternp=Pattern.compile(mailreg); while((line=bin.readLine())!=null) { Matcherm=p.matcher(line);//将模式与字符串关联 if(m.find()) { System.out.println(m.group()); } //System.out.println(line); } } }原文链接:https://www.f2er.com/regex/361503.html