java抓取全国城市空气质量jsoup
程序员文章站
2022-05-19 13:16:55
...
常量值:
public static final String[] CITYS = new String[] {"宜宾市","泸州市","重庆市","宜昌市","荆州市","岳阳市","武汉市"
,"黄石市","九江市","上饶市","安庆市","铜陵市","芜湖市","马鞍山市","南京市","镇江市","常州市","南通市","上海市"};
public static final String[] RIVERS = new String[] {"淮河流域","长江流域","太湖流域","巢湖流域"};
public final static SimpleDateFormat SDF0 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public final static SimpleDateFormat SDF1 = new SimpleDateFormat("HH:mm:ss");
public final static SimpleDateFormat SDF2 = new SimpleDateFormat("yyyy-MM-dd");
public final static SimpleDateFormat SDF3 = new SimpleDateFormat("yyyy年MM月dd日HH时");
工具类:
public class ToolsUtil {
private final static Logger logger = LoggerFactory.getLogger(ToolsUtil.class);
/**
* string 转日期
*/
public static Date s2Date(SimpleDateFormat sdf , String s) {
Date date = null;
try {
date = sdf.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/** 日期格式转换
*/
public static Date d2Date(SimpleDateFormat sdf , Date d) {
Date date = null;
try {
date = sdf.parse(sdf.format(d));
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* 两个list 合并为json
*/
public static JsonObject list2Json(List<String> k, List<String> v) {
JsonObject jsonObj = new JsonObject();
for(int i=0;i<k.size();i++) {
jsonObj.addProperty(k.get(i), v.get(i));
}
return jsonObj;
}
}
详细代码见下面,使用jsoup:
public class QualityMonitorServiceImpl{
private static final Object PRESENT = new Object();
private static final String airQUrl = "http://datacenter.mep.gov.cn/aqiweb2/";
private final static Logger logger = LoggerFactory.getLogger(QualityMonitorServiceImpl.class);
public Map<String, Object> extractAirQuality() {
Map<String,Object> cityMap= new HashMap<>();
try {
for(String c : CITYS) {
cityMap.put(c, PRESENT);
}
Connection conn = Jsoup.connect(airQUrl);
Document doc = conn.get();
String time = doc.select("p.tab_bg i.fr.time").text();
logger.info("time>>>[{}]",time);
Element tHead = doc.select("div.tab_content ul.list_title").get(0);
List<String> head = tHead.select("li").eachText();
head.remove(0);
head.add("quality");
logger.info("head>>>[{}]",head);
Elements tbody = doc.select("#legend_01_table tr");
for(Element e :tbody) {
List<String> content = e.select("td").eachText();
String city = content.remove(0);
if(cityMap.get(city) != null) {
content.add(AirQEnum.getAirQ(content.get(0)).getName());//AQI转优良...
JsonObject jsonObj = ToolsUtil.list2Json(head, content);
cityMap.put(city, jsonObj);
}
}
Date d=ToolsUtil.s2Date(SDF3, time);
Date date = ToolsUtil.d2Date(SDF2, d);
String updTime = SDF1.format(d);
cityMap.put("date", date);
cityMap.put("updTime", updTime);
} catch (Exception e) {
logger.error("抓取空气质量报错[{}]",e);
return null;
}
logger.info("cityAirQMap>>>[{}]",cityMap);
return cityMap;
}
}