import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class HtmlUtil {
public static HttpGet newHttpGet(String url){
HttpGet httpGet = new HttpGet(url);
httpGet.setConfig(RequestConfig.custom()
.setConnectionRequestTimeout(100000000 * 10) //连接池超时
.setConnectTimeout(1000000 * 10) //连接超时
.setSocketTimeout(10000000 * 10) // 读取超时
.build());
httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36");
return httpGet;
}
//七天 urls为城市编号集合
public static List<List<Day7>> getResult(List<Integer> urls) {
ArrayList<List<Day7>> list = new ArrayList<>();
for (Integer u : urls){
String url = "http://www.weather.com.cn/weather/"+u+".shtml";
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
try (CloseableHttpResponse response = httpClient.execute(HtmlUtil.newHttpGet(url))) {
String result = EntityUtils.toString(response.getEntity(),"utf-8"); //设置编码,防止乱码
List<Day7> listDay=new ArrayList<>();
Document document= Jsoup.parse(result);
Elements elements;
for (int i = 0; i < 7; i++) {
Day7 day7 = new Day7();
day7.setIp(u);
elements=document.select("h1");
String text=elements.get(i).text();
day7.setDate(text.substring(0,text.length()-4));
day7.setDay(text.substring(text.length()-3,text.length()-1));
elements=document.select("p[class=wea]");
day7.setWeather(elements.get(i).text());
elements=document.select("p[class=tem]");
day7.setHighTemp(elements.get(i).text());
day7.setLowTemp(elements.get(i).text());
elements=document.select("p[class=win]");
Element child = elements.get(i).child(0);
day7.setWindDirection1(child.select("span").get(0).attr("title"));
day7.setWindDirection2(child.select("span").get(0).attr("title"));
day7.setWindSpeed(elements.get(i).select("i").text());
listDay.add(day7);
}
list.add(listDay);
listDay.forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
return list;
}
//实况
public static List<WeatherFacts> execute(List<Integer> urls) {
ArrayList<WeatherFacts> list = new ArrayList<>();
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
for (Integer u : urls){
String url = "http://d1.weather.com.cn/sk_2d/"+u+".html?_=" + System.currentTimeMillis();
HttpGet hg = HtmlUtil.newHttpGet(url);
hg.setHeader("Referer",
"http://www.weather.com.cn/weather1dn/"+u+".shtml");
String line = "";
String Sline = "";
try {
CloseableHttpResponse response = httpClient.execute(hg);
BufferedReader bufferedreader = new BufferedReader(
new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
while ((line = bufferedreader.readLine()) != null) {
Sline += line+ "\n";
}
Sline = Sline.substring(Sline.indexOf('{'));
WeatherFacts weatherFacts = JSONObject.parseObject(Sline, WeatherFacts.class);
list.add(weatherFacts);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
list.stream().forEach(System.out::println);
return list;
}
//后24小时
public static List<List<Observer24>> Observer24(List<Integer> urls){
List<List<Observer24>> list = new ArrayList<>();
for (Integer u : urls) {
String url = "http://www.weather.com.cn/weather/" + u + ".shtml";
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
try (CloseableHttpResponse response = httpClient.execute(HtmlUtil.newHttpGet(url))) {
String result = EntityUtils.toString(response.getEntity(), "utf-8"); //设置编码,防止乱码
Document document = Jsoup.parse(result);
Elements script = document.getElementsByTag("script");
for (Element e : script) {
if (e.data().contains("var observe24h_data = ")) {
String data = e.data().replace("var observe24h_data = ", "");
//拿到od2
String s = JSONObject.parseObject(JSONObject.parseObject(data).get("od").toString()).get("od2").toString();
List<Observer24> observer24s = JSONArray.parseArray(s, Observer24.class);
list.add(observer24s);
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
list.stream().forEach(System.out::println);
return list;
}
}
@Data
public class Day7 {
Integer ip; //地区编号
String name;
String date; //日期
String day;
String weather; //天气
String highTemp; //高温
String lowTemp; //低温
String windDirection1; //风向1
String windDirection2; //风向2
String windSpeed; //风速(级)
}
@Data
public class Observer24 {
private String od21; //时间
private String od22; //温度
private String od23;
private String od24; //风向
private String od25; //风力(级)
private String od26; //降水量 mm
private String od27; //相对湿度 %
private String od28;
}
@Data
public class WeatherFacts {
String tempf; // 华氏度
String date; //日期
String rain; //降水
String limitnumber; //
String temp; //温度实况 -15.3C
String city;// 城市编号
String cityname; //地区名
String wse; //风速 3km/h
String qy; //气压
String aqi_pm25; //pm2.5
String njd;//能见度
String nameen; //地区名 英文
String weathere; //天气英文
String WD; //风向
String SD; //湿度
String sd; //:"91%",
String rain24h; //日降水
String weathercode; //天气代号
String aqi; //pm2.5
String weather; //天气中文
String wde;//风向符号
String time;//时间
String WS;//风速(级)
}
java获取中国天气网数据(24小时 7天 实况)
于 2023-11-09 15:47:03 首次发布