《黑马程序员》 javaweb网页爬虫技术的实现

来源:互联网 发布:白银交易软件下载 编辑:程序博客网 时间:2024/06/10 05:13
------- android培训、java培训、期待与您交流! ----------
package cn.itcast.p6.regex;import java.io.BufferedReader;import java.io.FileReader;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 RegexDemo6 { /**  * @param args  */ /**  *网页爬虫技术:   *  其实就是一个程序在网页中获取符合指定规则的数据  *  爬取邮箱地址  *     A本地文件或网络中的m  * @throws IOException   * */ public static void main(String[] args) throws IOException {  List<String> listu=getMails_3();  for(String li:listu){   System.out.println(li);  } }  public static List<String> getMails_3() throws IOException{  //获取网站中的邮箱:  URL url=new URL("http://192.168.123.209:8080/myweb/test.html");    //设定源  BufferedReader br=new BufferedReader(new InputStreamReader(url.openStream()));    //将读取到的数据存储在集合中  List<String> list=new ArrayList<String>();  String line=null;  //定义规则//  String regex="[a-zA-Z0-9]+@[a-z0-9]+(\\.[a-zA-Z0-9]{1,3})+";//  String regex="[a-zA-Z0-9]+@[a-z0-9]+(\\.[a-zA-Z0-9]{1,3})+";  String regex="\\w+@\\w+(\\w+.)+";  Pattern p=Pattern.compile(regex);  while((line=br.readLine())!=null){   //把读取到的数据存储在集合中   Matcher m=p.matcher(line);   while(m.find()){    list.add( m.group());   }  }  return list; }   public static List<String> getMails_2() throws IOException{  //设定源  BufferedReader br=new BufferedReader(new FileReader("d:\\a.txt"));    //将读取到的数据存储在集合中  List<String> list=new ArrayList<String>();  String line=null;  //定义规则//  String regex="[a-zA-Z0-9]+@[a-z0-9]+(\\.[a-zA-Z0-9]{1,3})+";//  String regex="[a-zA-Z0-9]+@[a-z0-9]+(\\.[a-zA-Z0-9]{1,3})+";  String regex="\\w+@\\w+(\\w+.)+";  Pattern p=Pattern.compile(regex);  while((line=br.readLine())!=null){   //把读取到的数据存储在集合中   Matcher m=p.matcher(line);   while(m.find()){    list.add( m.group());   }  }  /*  //对集合进行遍历,取出数据  for(String url:list){   System.out.println(url);  }*/  return list;   }

0 0
原创粉丝点击