中国灾害预警爬取解析

有获取灾害信息提醒的需求,但是发现并没有开发的API 。已有的API都需要付钱。
天气预报API,可以看知乎的这边文章。网上的天气 API 哪一个更加可靠?
里面的普通的天气预报部分免费,但是灾害预警的API都是需要收费的。于是自己去摸索了。最后通过发F12发现了中国天气网中可用的请求:
http://www.weather.com.cn/data/alarm_xml/alarminfo.xml

顺便写了下解析使用工具,如下:

@Data
@XmlRootElement(name = "AlermInfo")
public class AlarmInfo {
    private AlarmDetail roadIcing;
    private AlarmDetail snowStorm;
    private AlarmDetail rainStorm;
    private AlarmDetail hail;
    private AlarmDetail gale;
    private AlarmDetail heavyFog;
    private AlarmDetail heatWave;
    private AlarmDetail drought;
    private AlarmDetail coldWave;
    private AlarmDetail sWHazards;
    private AlarmDetail lightning;
    private AlarmDetail haze;
    private AlarmDetail sandStorm;
    private AlarmDetail frost;
    private AlarmDetail typhoon;
    private AlarmDetail other;
}

@Data
public class AlarmDetail {
    List<Station> station;
}

@Data
public class Station {
    @JacksonXmlProperty(isAttribute = true, localName = "stationId")
    private String stationId;
    @JacksonXmlProperty(isAttribute = true, localName = "areaId")
    private String areaId;
    @JacksonXmlProperty(isAttribute = true, localName = "stationName")
    private String stationName;
    @JacksonXmlProperty(isAttribute = true, localName = "lon")
    private String lon;
    @JacksonXmlProperty(isAttribute = true, localName = "lat")
    private String lat;
    @JacksonXmlProperty(isAttribute = true, localName = "signalType")
    private String signalType;
    @JacksonXmlProperty(isAttribute = true, localName = "signalLevel")
    private String signalLevel;
    @JacksonXmlProperty(isAttribute = true, localName = "issueTime")
    private String issueTime;
    @JacksonXmlProperty(isAttribute = true, localName = "relieveTime")
    private String relieveTime;
    @JacksonXmlProperty(isAttribute = true, localName = "issueContent")
    private String issueContent;

}


public class JacksonSerialize implements SerializeDelegate {
    private static final Logger LOGGER = LoggerFactory.getLogger(JacksonSerialize.class);
    private static final ObjectMapper OBJECT_MAPPER;
    private static final XmlMapper XML_MAPPER;

    static {
        OBJECT_MAPPER = new ObjectMapper();
        OBJECT_MAPPER.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
        OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);

        JacksonXmlModule module = new JacksonXmlModule();
        module.setDefaultUseWrapper(false);
        XML_MAPPER  = new XmlMapper(module);
        XML_MAPPER.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);
        XML_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        XML_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        XML_MAPPER.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
    }

    @Override
    public <T> T jsonToBean(String json, Class<T> clazz) {
        T result = null;
        try {
            result = OBJECT_MAPPER.readValue(new ByteArrayInputStream(json.getBytes(Const.Charset.UTF_8)), clazz);
        } catch (IOException e) {
            LOGGER.error("Convert json to bean " + clazz.getName() + " error.", e);
        }
        return result;
    }

    @Override
    public String beanToJson(Object object) {
        String result = null;
        try {
            result = OBJECT_MAPPER.writeValueAsString(object);
        } catch (Exception e) {
            LOGGER.error("Convert bean" + object.getClass().getName() + " to json error.", e);
        }
        return result;
    }

    @Override
    public <T> T xmlToBean(String xml, Class<T> c) {
        T t = null;
        try {
            t = XML_MAPPER.readValue(new ByteArrayInputStream(xml.getBytes(Const.Charset.UTF_8)), c);
        } catch (IOException e) {
            LOGGER.error("Convert xml to bean " + c.getName() + " error.", e);
        }
        return t;
    }

    @Override
    public String beanToXml(Object obj) {
        String result = null;
        try {
            result = XML_MAPPER.writeValueAsString(obj);
        } catch (Exception e) {
            LOGGER.error("Convert bean" + obj.getClass().getName() + " to xml error.", e);
        }

        return result;
    }
}
发布了37 篇原创文章 · 获赞 58 · 访问量 16万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览