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


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

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

  • 相关阅读:
    全链路压测(1):认识全链路压测
    碎片式的技术笔记
    聊聊传统压测和全链路压测的区别
    生产全链路压测常态化方案
    C++选择文件打开方式的函数
    常用的一些 git 命令
    MyBatis 批量插入数据的 3 种方法!
    MyBatis Plus 批量数据插入功能,yyds!
    什么是可中断锁?有什么用?怎么实现?
    1.3w字,一文详解死锁!
  • 原文地址:https://www.cnblogs.com/jamsbwo/p/4109114.html
Copyright © 2020-2023  润新知