网页爬虫小程序

一个简单的网页爬虫程序:

/**
 * 网页爬虫 :
 *      得到网页上的邮箱地址  
 *      得到网页上的时间戳
 */
public class RegexDemo
{
    public static void main(String[] args) throws IOException
    {
        String url_1 = "http://blog.sina.com.cn/s/blog_515617e60101e151.html";
        String url_2 = "http://bbs.tianya.cn/post-934-163735-1.shtml";

        //邮箱地址
        String reg_1 = "\\w+@\\w+(\\.\\w+)+";

        //格式为这样的时间戳:2017-10-25 08:41:27
        String reg_2 = "(\\d+-){2}\\d+ (\\d+:){2}\\d+";

        webCrawler(url_1, reg_1);//得到网页上的邮箱地址
        webCrawler(url_2,reg_2); //得到网页上的时间戳
    }
    /**
     * 
     * @param str :网址
     * @param regex :正则表达式
     * @throws IOException
     */
    public static void webCrawler(String str,String regex) throws IOException
    {
        URL url = new URL(str);
        URLConnection conn = url.openConnection();
        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line = null;
        Pattern pattern = Pattern.compile(regex);
        while((line = br.readLine()) != null)
        {
            Matcher matcher = pattern.matcher(line);
            while(matcher.find())
            {
                System.out.println(matcher.group());
            }
        }
        br.close();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值