zoukankan      html  css  js  c++  java
  • 从一个网页上摘取想要的元素

    示例:从网页上摘取页面中的所有邮箱

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.InputStreamReader;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class Test {
        public static void main(String[] args) throws Exception {
            // 1.1 创建一个url对象
            URL url = new URL(
                    "https://www.douban.com/group/topic/41562980/?start=500");
            // 1.2打开链接
            URLConnection conn = url.openConnection();
            // 1.3 设置连接网络超时时间 单位为毫秒
            conn.setConnectTimeout(1000 * 10);
            // 1.4 通过流 操作读取指定网络地址中的文件
            BufferedReader bufr = new BufferedReader(new InputStreamReader(
                    conn.getInputStream()));
            String line = null;
            // 1.5 匹配email的正则
            String regex = "[a-zA-Z0-9_-]+@\w+\.[a-z]+(\.[a-z]+)?";
            // 1.6 使用模式的compile()方法生成模式对象
            Pattern p = Pattern.compile(regex);
            // 1.
            while ((line = bufr.readLine()) != null) {
                Matcher m = p.matcher(line);
                while (m.find()) {
                    System.out.println(m.group());// 获得匹配的email
                }
            }
        }
    }
  • 相关阅读:
    angular ngIf指令 以及组件的输入输出
    angular 命令行指令总结
    angular8.x 事件的处理和样式绑定
    nodejs更新版本(windows)
    angular重要指令 ngFor
    emmet 常用总结
    手机真机调试 (ng项目)
    最长回文子串
    最长连续序列
    重复的子字符串
  • 原文地址:https://www.cnblogs.com/alexanderthegreat/p/6936992.html
Copyright © 2011-2022 走看看