Java获取天气预报工具类
最近博主真的是太忙了,公司内的业务太多,做不完天天加班,没时间写博客了,所以趁着这稍微轻松点的日子写俩篇最近个人博客需求所用到的一些小技巧。解释什么的都写在代码注释上面,有不懂的小伙伴可以加群咨询
本文测试所采集的数据来自 中国天气网(http://www.weather.com.cn/weather/101270101.shtml),
{
.....
//在前面的一些代码主要是为了获取bm--城市编号
bm = it.next();
String url = "http://www.weather.com.cn/weather/" + bm + ".shtml";
try {
//用的jsoup去解析获取到的页面,这是一个非常好用的工具包
Document doc = Jsoup.connect(url).get();
//根据class或者id 解析这个div内的数据
Elements content = doc.getElementsByClass("con today clearfix");
for (Element e : content) {
int i=0;
//获取body内指定class内容
Document conDoc = Jsoup.parse(e.toString());
//获取列表class内容
Elements sky = content.select("li[class^=sky skyid lv]");
//这里的类是我自己定义的,方便后面进行批量存储和修改
List<WeatherDays> updates = new ArrayList<>();
List<WeatherDays> inserts = new ArrayList<>();
//将li标签内的文本遍历处理
for (Element sk : sky) {
//拿到数据以后就是开始拆分获取需要的数据了,后面的就是我存储7天天气预报的一个过程
WeatherDays weatherDays = new WeatherDays();
String [] ys = sk.text().split(" ");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
String getTime = simpleDateFormat.format(new Date());
String time;
//除开今天未来6天时间
if(i<=6){
time = DateUtil.getBeforeDate(0,0,i,getTime);
i++;
}else {
i = 0;
time = DateUtil.getBeforeDate(0,0,i,getTime);
i++;
}
Date date = simpleDateFormat.parse(time);
String timeForWorkType = format.format(date);
//此处判断日期工作日 休息日节假日工具--如果有需要可以咨询,后面人多的话我再单独写出来吧。
int workType = HolidayUtil.request(timeForWorkType);
weatherDays.setCityId(bm);
//时间
weatherDays.setTime(time);
weatherDays.setWeatherStatus(ys[1]);
if(ys.length<5){
weatherDays.setMax(Integer.parseInt(ys[2].split("℃")[0].split("/")[0]));
weatherDays.setMin(Integer.parseInt(ys[2].split("℃")[0].split("/")[0]));
}else {
weatherDays.setMax(Integer.parseInt(ys[2].split("℃")[0].split("/")[0]));
weatherDays.setMin(Integer.parseInt(ys[2].split("℃")[0].split("/")[1]));
}
weatherDays.setWorkType(workType);
WeatherDays findResult = spiderWeatherService.findByCondition(weatherDays);
if(findResult!=null){
weatherDays.setId(findResult.getId());
weatherDays.setUpdatedAt(new Date());
updates.add(weatherDays);
}else {
weatherDays.setCreatedAt(new Date());
inserts.add(weatherDays);
}
//休眠一会儿给方法 一些缓冲时间
Thread.sleep(100);
}
if(updates.size()>0){
spiderWeatherService.batchUpdateWeather(updates);
}if(inserts.size()>0){
spiderWeatherService.batchInsertWeather(inserts);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
用到的一些类
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.intelligent.building.cloud.job.entity.predict.WeatherDays;
import org.intelligent.building.cloud.job.service.SpiderWeatherService;
import org.intelligent.building.cloud.job.util.DateUtil;
import org.intelligent.building.cloud.job.util.HolidayUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.10.2</version>
</dependency>
正文到此结束