代码编写中经常会涉及的工具类

欢迎加群574337670

这段时间,应着老板的要求,从无到有,从无从下手,到一步步摸索,终于顺利完成了老板交代的任务,在这次任务当中,学会了很多新知识,在这里,我将总结一下我在此次任务中经常用到的一些工具类,我认为这些工具类,有必要保存一份,这样,不至于到了编码的时候花更多时间去网上找,虽然我这也算在网上找的,但花了我不少时间,为了下次不再浪费更多时间,在此我将把一些常用的工具类分享出来。

我这次做的主要任务是,从网页抓取数据,数据量很大,抓取几个省的天气数据,这当中涉及如何抓取数据,如何寻找可以被我们轻松解析的数据,抓到了数据如何解析,抓到了数据如何存储数据,如何设计表结构,如何将数据存储数据库后能够方便拿出来,等等,这当中涉及的知识点真的不少,说针真的,这是我第一从网页抓取数据,起初,老板给我布置了这个任务,我有点不知所措,不知如何下手,但是没有办法,我硬着头皮跟师兄请教,逼着自己学习,总算有效。

/**
     * 根据当前时间获取上一月的同一时间
     * @return 
     */
    public static String lastMonth(){
        Calendar now = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
        now.setTime(new Date());
        int day = now.get(Calendar.DATE);
        if(1 == day){
            now.add(Calendar.MONTH, -1);//获取上一个月份
        }
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        return format1.format(now.getTime());
    }

/***
     * 根据时间查询是星期几,
     * 
     * @param ctime yyyy-MM-dd
     * @return
     */
    public static String praseWeeks(String ctime) {
        Date ctimeDate = DateUtils.parseDate(ctime, DateUtils.SHOW_DATE_FORMAT);
        String[] weekOfDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        Calendar calendar = Calendar.getInstance();
        if (ctimeDate != null) {
            calendar.setTime(ctimeDate);
        }
        int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return weekOfDays[w];
    }
/***
     * 获取15年1月到17年3月的年月字符串集合
     */
    public static List<String> acceptXingQi() {
        List<String> list = new ArrayList<String>();
        String startdate = "2014-12-01";
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date d1;
        try {
            d1 = format.parse(startdate);
            Calendar c1 = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
            c1.setTime(d1);
            for (int i = 0; i < 26; i++) {
                c1.add(Calendar.MONTH, 1);//当前的月份加1
                SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
                list.add(format1.format(c1.getTime()));
            }
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
        return list;
    }
/**
     * 获取月份的天数
     * 
     * @return
     */
    public static int getDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        return cal.getActualMaximum(Calendar.DATE);
    }
/**
     * 比较两个日期之间天数
     * 
     * @param fDate
     * @param oDate
     */
    public static int daysOfTwo(Date fDate, Date toDate) {
        return (int) ((toDate.getTime() - fDate.getTime()) / (1000 * 60 * 60 * 24));

    }

 我们再抓取网页数据的时候,经常会遇到以下这样一个问题.403拒绝访问,想必大家这个时候一定非常蛋疼,这访问不了,那数据又改如何抓取呢?这里,我来抛砖引玉,先介绍大家一种方法,解决这样的问题

其实这种问题解决起来也不难,这只不过网站管理员耍的一种小把戏。没什么难度。大家找到你抓取数据时网页的路劲,打开请求他Header,可以发现有这样一个键值对  Referer:http://www.weather.com.cn/weather40d/101310201.shtml ,

没错就是它,就是它,问题就很容易解决了,现在我把主要的抓取数据代码分享出来。

long timeMillis = System.currentTimeMillis();
        String url = "http://d1.weather.com.cn/calendar_new/2017/101280101_201703.html?_=1490061939993";//这是你的网页路径
        String reqUrl = "http://d1.weather.com.cn/calendar_new/2017/101280101_201704.html?_="+ timeMillis;//1.将后边的时间戳替换
        reqUrl = reqUrl.substring(0, reqUrl.indexOf("=", 60) + 1) + timeMillis;
        Pattern JSON_PATTERN = Pattern.compile("fc40\s*=\s*(\[(\S*\s*\S*)*\])", Pattern.DOTALL);//2.正则表达式匹配字符串
        String rsp = "";
        Map<String, String> requests = Maps.newHashMap();
        requests.put("Referer","http://www.weather.com.cn/weather40d/101280101.shtml");//3.这一步是最重要的,有了它就能正常访问网页了
        try {
            rsp = WebUtils.doGet(reqUrl, null, null, requests);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String jsonStr = null;
        Matcher m = JSON_PATTERN.matcher(rsp);
        while (m.find()) {
            jsonStr = m.group(1);
        }
WebUtils是我们封装的工具类。doGet方法就是请求网页数据,代码都是原生代码,出于保密,我就不贴出来了,此处最重要的一步就是第3步

获取到了数据,但是还是可能会发生你意想不到的问题,比如乱码,这个乱码比较特殊,不像一般的乱码,这种数据采用的是gzip压缩后的格式,所以处理起来就不能想普通乱码那样子去解决。你得解压之后才能去设置编码格式.具体的代码如下

/**
     * 乱码解决
     */
    @Test
    public void testLuanMa() {
        String htmlContent = "";
        java.io.InputStream inputStream = null;
        java.net.URL url = null;
        try {
            url = new java.net.URL("http://tianqi.2345.com/t/wea_history/js/201703/59289_201703.js");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        java.net.HttpURLConnection connection = null;
        try {
            connection = (java.net.HttpURLConnection) url.openConnection();
        } catch (IOException e2) {
            e2.printStackTrace();
        }
        try {
            connection.connect();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        try {
            inputStream = connection.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte bytes[] = new byte[1024 * 100];
        int index = 0;
        int count = 0;
        try {
            count = inputStream.read(bytes, index, 1024 * 100);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        while (count != -1) {
            index += count;
            try {
                count = inputStream.read(bytes, index, 1);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            htmlContent = new String(bytes, "gb2312");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.out.println(htmlContent);
原文地址:https://www.cnblogs.com/yxh1008/p/6661999.html