您的位置:首页 > 其它

正则表达式—网页爬虫

2014-07-11 20:05 239 查看
/**
* @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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: