zoukankan      html  css  js  c++  java
  • 走过路过不要错过~教你用java抓取网页中你想要的东东~~

    学习了正则之后,打算用java玩一玩,所以就决定用它来实现一个好玩的idea

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    //和网络相关的操作
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    /**
     * 正则抓取邮箱
     * @author 大鹏
     *
     */
    public class Test {
      public static void main(String[] args) throws IOException {
         //1.1   我们首先来创建一个url对象
          URL url = new URL("//这里需要写一个有邮箱地址的网页");  
         //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 匹配邮箱地址的正则,看不懂没关系只要知道是匹配邮箱地址的正则
          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  
              }  
          }  
      }
    }
    抓网页当中的所有邮箱地址
  • 相关阅读:
    LeetCode-Cycle Detection,Find the Duplicate Number
    LeetCode-Symmetric Tree
    剑指offer-打印链表倒数第k个结点
    Http协议中Get和Post的区别
    ORDER BY 语句
    AND 和 OR 运算符
    WHERE 子句
    SQL SELECT DISTINCT 语句
    SQL SELECT 语句
    SQL DML 和 DDL
  • 原文地址:https://www.cnblogs.com/System-out-println/p/5532192.html
Copyright © 2011-2022 走看看