zoukankan      html  css  js  c++  java
  • 一个采集邮箱的网络爬虫(听毕老师讲的)

    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;
        }
    }    

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

  • 相关阅读:
    玲珑学院-ACM比赛1014
    扩展欧几里得算法
    中国剩余定理(孙子定理)及实现----原理详解
    搞懂树状数组
    HDU3792---Twin Prime Conjecture(树状数组)
    树状数组 模板
    HDU1541--Stars(树状数组)
    HDU4046--Panda(树状数组)
    CCF-201604-1-折点计数
    CCF-201604-2-俄罗斯方块
  • 原文地址:https://www.cnblogs.com/jamsbwo/p/4109114.html
Copyright © 2011-2022 走看看