正则表达式—网页爬虫
发布时间:2020-12-14 01:42:41 所属栏目:百科 来源:网络整理
导读:/** * @param args * 网页爬虫,其实就是一个程序用于在互联网中获取符合指定规则的数据 * @throws IOException */public static void main(String[] args) throws IOException {ListString mailList=test();for(String mail:mailList){System.out.println(m
/** * @param args * 网页爬虫,其实就是一个程序用于在互联网中获取符合指定规则的数据 * @throws IOException */ public static void main(String[] args) throws IOException { List<String> mailList=test(); for(String mail:mailList){ System.out.println(mail); } } public static List<String> test() throws IOException{ //创建一个集合容器 List<String> list=new ArrayList<String>(); //创建一个URL对象,获取流 URL url=new URL("file:///E:/WorkspaceForJava/test1/myWeb.html"); BufferedInputStream bis=new BufferedInputStream(url.openStream()); //创建一个字节数组,将从网页中读取到的内容写到这个数组中 byte[] buf=new byte[1024*4]; int ch=0; while((ch=bis.read(buf))!=-1){ String text=new String(buf,ch); //编写邮编正则表达式 String regex="[a-zA-Z0-9_]+@[a-zA-Z]+(.[a-zA-Z]{1,3})+"; //将符合正则表达式的内容存到集合容器中 Pattern p=Pattern.compile(regex); Matcher m=p.matcher(text); while(m.find()){ String mail=m.group(); list.add(mail); } } return list; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读