JAVA题目笔记(十三) 爬虫

一、网络爬取

java 复制代码
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;


public class Main {
    public static void main(String[] args) throws CloneNotSupportedException, IOException {
        //创建URL对象
        URL url=new URL("https://mbd.baidu.com/newspage/data/landingsuper?context=%7B%22nid%22%3A%22news_9556566747732783179%22%7D&n_type=-1&p_from=-1");
        //连接网络
        URLConnection conn=url.openConnection();
        //创建对象去读取网络中的数据
        BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        //获取正则表达式的对象 pattern
        String regex=""; //正则表达式
        Pattern pattern =Pattern.compile(regex);
        while((line=br.readLine())!=null){
            //拿着文本匹配器的对象matcher按照pattern的规则去读取当前的这一行信息
            Matcher matcher=pattern.matcher(line);
            while(matcher.find()) {
                System.out.println(matcher.group());
            }
        }
        br.close();
    }
}

二、本地爬取

java 复制代码
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;


public class Main {
    public static void main(String[] args) throws CloneNotSupportedException, IOException {
        //爬取文本中对应数据:电话、邮箱、手机号、热线
        //手机号
        String regex1="1[3-9]\\d{9}";
        //邮箱
        String regex2="\\w+@[\\w&&[^_]]{2,6}(\\.[a-zA-Z]{2,3}){1,2}";
        //座机
        String regex3="0\\d{2,3}-?[1-9]\\d{4,9}";
        //热线电话
        String regex4="400-?[1-9]\\d{2}-?[1-9]\\d{3}";

        //正则表达式整合
        String regex5="(1[3-9]\\d{9})|(\\w+@[\\w&&[^_]]{2,6}(\\.[a-zA-Z]{2,3}){1,2})"+
                "|(0\\d{2,3}-?[1-9]\\d{4,9})|(400-?[1-9]\\d{2}-?[1-9]\\d{3})";

        String s="来黑马程序员学习Java,"+
        "电话:18512516758,18512508907"+
        "或者联系邮箱:boniu@itcast.cn,"+
        "座机电话:01036517895,010-98951256"+
        "邮箱:bozai@itcast.cn, 热线电话:400-618-9090,400-618-4000,4006184000,4006189090";

        Pattern p=Pattern.compile(regex5);

        Matcher m=p.matcher(s);


       while(m.find()){
           String str=m.group();
           System.out.println(str);
        }
    }
}
相关推荐
秉承初心5 小时前
Java 23种设计模式的详细解析
java·设计模式
程序员大雄学编程5 小时前
「深度学习笔记1」深度学习全面解析:从基本概念到未来趋势
人工智能·笔记·深度学习
千码君20165 小时前
Go语言:记录一下Go语言系统学习的第一天
java·开发语言·学习·golang·gin·并发编程·编译语言
聪明的笨猪猪5 小时前
Java 面试清单(含超通俗生活案例与深度理解)
java·经验分享·笔记·面试
MrSYJ5 小时前
学完涨工资的技巧2:Spring Authorization Server如何签发JWTToken
java·spring boot·微服务
珹洺5 小时前
Java-Spring入门指南(二十六)Android Studio下载与安装
java·spring·android studio
JAVA学习通5 小时前
JDK高版本特性总结与ZGC实践
java·jvm·算法
cxyxiaokui0016 小时前
JDK 动态代理 vs CGLIB:原理、区别与 Spring AOP 底层揭秘
java·后端·spring
骁的小小站6 小时前
Learn C the Hardway学习笔记和拓展知识(一)
c语言·开发语言·c++·经验分享·笔记·学习·bash
代码充电宝6 小时前
LeetCode 算法题【中等】189. 轮转数组
java·算法·leetcode·职场和发展·数组