一个采集邮箱的网络爬虫(听毕老师讲的)

public class NetSplider {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
    /**
    * 网络爬虫:根据一定的规则从网络源或者本地源寻找并挖掘与规则相匹配的数据 1,读取文件,通过本地文件或者网络url 2,匹配并挖掘数据
    */
    // 定义规则(正则表达式)
    String regex = "\\w+@\\w+(\\.\\w+)+";
    // List<String> listByLocal=getMailByLocal(regex);
    List<String> listByNet = getMailByNet(regex);
    PrintWriter pw=new PrintWriter((new     FileWriter("G:\\workstation\\正则表达式\\src\\案例\\list.txt")),true);
    //    BufferedWriter bw = new BufferedWriter(new FileWriter(
    //    "G:\\workstation\\正则表达式\\src\\案例\\list.txt"));
    for (String mail : listByNet) {
        if(mail != null) {
            pw.println(mail);
            //pw.flush();
            //bw.write(mail);
        }
        System.out.println(mail);
    }
}

// 通过给出的url挖掘数据
private static List<String> getMailByNet(String regex) throws IOException {
    // 给出url,并定义URL对象
    String str_url = "http://bbs.tianya.cn/post-enterprise-401802-1.shtml";// 在此输入url地址
    URL url = new URL(str_url);

    // 打开url链接
    URLConnection uc = url.openConnection();
    // 获取并读取数据流
    BufferedReader buffin = new BufferedReader(new InputStreamReader(uc.getInputStream()));

    // 获取功能,定义列表存储数据
    Pattern p = Pattern.compile(regex);
    List<String> list = new ArrayList<String>();
    String line = null;
    while ((line = buffin.readLine()) != null) {
        Matcher m = p.matcher(line);
        while (m.find()) {
            list.add(m.group());
        }
    }
    // 关闭资源
    buffin.close();
    return list;
}

// 通过给出本地路径挖掘数据
private static List<String> getMailByLocal(String regex) throws IOException {
    // 读取流定义并读取文件
    BufferedReader buffin = new BufferedReader(new FileReader(""));// 在此输入本地文件名
    String line = null;

    // 获取功能
    Pattern p = Pattern.compile(regex);
    List<String> list = new ArrayList<String>();
    while ((line = buffin.readLine()) != null) {
        Matcher m = p.matcher(line);
        while (m.find()) {
            list.add(m.group());
        }
    }
    buffin.close();
    return list;
    }
}    

根据本地和网络资源获得邮箱

转载于:https://www.cnblogs.com/jamsbwo/p/4109114.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值