zoukankan      html  css  js  c++  java
  • 定时器2分钟爬一次数据添加到Mysql(给一个网址到上面去爬该网址的数据)

    package com.nongyuanbao.server;
    
    import java.io.IOException;
    import java.util.List;
    
    import org.apache.http.HttpResponse;
    import org.apache.http.client.config.RequestConfig;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;
    
    import com.alibaba.fastjson.JSON;
    import com.jfinal.aop.Before;
    import com.jfinal.plugin.activerecord.tx.Tx;
    import com.neptune.core.project.AutoIdHelper;
    import com.nongyuanbao.vo.SrtResultVO;
    import com.nongyuanbao.weather.model.Report;
    
    
    public class WeatherReportServer {
    
        /**
         * 一个小时爬30次数据
         */
        @Before(Tx.class)
        public void addWeatherreport(){
            CloseableHttpClient httpCilent2 = HttpClients.createDefault();
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(5000)   //设置连接超时时间
                    .setConnectionRequestTimeout(5000) // 设置请求超时时间
                    .setSocketTimeout(5000)
                    .setRedirectsEnabled(true)//默认允许自动重定向
                    .build();
            HttpGet httpGet2 = new HttpGet("https://free-api.heweather.com/s6/weather/now?location=%E9%B2%81%E5%B1%B1&key=1d39bc6e725649dabebf1a3c1f27f4cc");
            httpGet2.setConfig(requestConfig);
            String srtResult = "";
            try {
                HttpResponse httpResponse = httpCilent2.execute(httpGet2);
                if(httpResponse.getStatusLine().getStatusCode() == 200){
                    srtResult = EntityUtils.toString(httpResponse.getEntity());//获得返回的结果
                    //srtResult.substring(srtResult.indexOf(":"),srtResult.lastIndexOf("}"));  
                    
                    List<SrtResultVO> srtResultList = JSON.parseArray( srtResult.substring(srtResult.indexOf(":")+1,srtResult.lastIndexOf("}")), SrtResultVO.class);
                    System.out.println(srtResultList);
                    for(SrtResultVO vo : srtResultList){
                        Report weatherreport = new Report();
                        weatherreport.setId(AutoIdHelper.getId());
                        weatherreport.setCid(vo.getBasic().getCid());
                        weatherreport.setLocation(vo.getBasic().getLocation());
                        weatherreport.setCity(vo.getBasic().getParent_city());
                        weatherreport.setProvince(vo.getBasic().getAdmin_area());
                        weatherreport.setCnty(vo.getBasic().getCnty());
                        weatherreport.setLat(vo.getBasic().getLat());
                        weatherreport.setLon(vo.getBasic().getLon());
                        weatherreport.setTz(vo.getBasic().getTz());
                        
                        weatherreport.setLoc(vo.getUpdate().getLoc());
                        weatherreport.setUtc(vo.getUpdate().getUtc());
                        
                        weatherreport.setCloud(vo.getNow().getCloud());
                        weatherreport.setCondCode(vo.getNow().getCond_code());
                        weatherreport.setCondTxt(vo.getNow().getCond_txt());
                        weatherreport.setFl(vo.getNow().getFl());
                        weatherreport.setHum(vo.getNow().getHum());
                        weatherreport.setPcpn(vo.getNow().getPcpn());
                        weatherreport.setPres(vo.getNow().getPres());
                        weatherreport.setTmp(vo.getNow().getTmp());
                        weatherreport.setVis(vo.getNow().getVis());
                        weatherreport.setWindDeg(vo.getNow().getWind_deg());
                        weatherreport.setWindDir(vo.getNow().getWind_dir());
                        weatherreport.setWindSc(vo.getNow().getWind_sc());
                        weatherreport.setWindSpd(vo.getNow().getWind_spd());
                        
                        weatherreport.setStatus(vo.getStatus());
                        
                        System.out.println(weatherreport);
                        weatherreport.save();
                        
                    }
                    
                    
                }else if(httpResponse.getStatusLine().getStatusCode() == 400){
                    //..........
                }else if(httpResponse.getStatusLine().getStatusCode() == 500){
                    //.............
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    httpCilent2.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    package com.nongyuanbao.task;
    
    import com.jfinal.aop.Duang;
    import com.nongyuanbao.server.WeatherReportServer;
    
    public class WeatherReportTask implements Runnable {
        
        @Override
        public void run() {
            WeatherReportServer weatherReportServer =  Duang.duang(WeatherReportServer.class);
            weatherReportServer.addWeatherreport();
        }
    
    
    }
    
    
    cron4j=weatherReport
    
    
    #demo.cron=* * * * *
    #demo.class=
    #demo.daemon=true
    #demo.enable=true 
    
    #两分钟运行一次
    weatherReport.cron=*/2 * * * * 
    weatherReport.class=com.nongyuanbao.task.WeatherReportTask
    weatherReport.daemon=true
    weatherReport.eable=true
    
    
    
    }
  • 相关阅读:
    as
    android 手势上下滑动头部自动隐藏View标签
    ListView 禁止滑动和点击
    Android corners 圆角属性各个版本之间兼容问题
    Android 解耦利器 EventBus
    Android 修改Activity标题样式 actionBar
    JavaScript 的写的grid Ajax加载数据 表格内增删改查 支持自定义分页大小 支持批量删除数据,支持按住ctrl多选
    Andorid 应用程序开启多进程
    比较两个文件是否相同(计算MD5效验码比较方式)
    Android 文件上传支持拍照录用录视频
  • 原文地址:https://www.cnblogs.com/wuaili/p/8652622.html
Copyright © 2011-2022 走看看