正则表达式—网页爬虫

	/**
	 * @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,0,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;
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值