> 文档中心 > Java从天气网爬取历史天气数据

Java从天气网爬取历史天气数据

    private static String URL = "http://lishi.tianqi.com";    private static String BaseURL = "http://www.tianqi.com";    public static Map getWeather(String city){ Map map = new HashMap(); String pinYin = Pinyin4jUtils.getPinYin(city); pinYin = pinYin.replace("shi",""); String yyMMdd = DateUtils.getFormat("yyyyMMdd"); Document document = null; try {      document = Jsoup.connect(URL + "/" + pinYin + "/" + yyMMdd + ".html").get();      //温度     Elements hisdaily = document.getElementsByClass("hisdailytemp");//     天气     Elements hisdailywea = document.getElementsByClass("hisdailywea");//     风向     Elements hisdailywind = document.getElementsByClass("hisdailywind");//     Elements children = elementById.children();//     Elements elements = children.get(1).children();     map.put("date",city);     map.put("temperature",hisdaily.get(0).text());     map.put("weather",hisdailywea.get(0).text());     map.put("wind",hisdailywind.get(0).text()); } catch (IOException e) {     e.printStackTrace(); } return map;    }    public static Map newGetWeather(String city){ Map map = new HashMap(); String pinYin = Pinyin4jUtils.getPinYin(city); pinYin = pinYin.replace("shi",""); Document document = null; try {     Connection con = Jsoup.connect(BaseURL + "/" + pinYin + "/").userAgent(      "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36")      .timeout(30000); // 设置连接超时时间     Connection.Response response = con.execute();     if (response.statusCode() == 200) {  document = con.get();     } else {  System.out.println(response.statusCode());  return new HashMap();     }     //温度     Elements hisdaily = document.getElementsByClass("now");//     天气     Elements hisdailywea = document.getElementsByClass("now");//     风向     Elements hisdailywind = document.getElementsByClass("shidu");//     Elements children = elementById.children();//     Elements elements = children.get(1).children();     map.put("date",city);     map.put("temperature",hisdaily.get(0).text());     map.put("weather",hisdailywea.get(0).nextElementSibling().text());     map.put("wind",hisdailywind.get(0).tagName("b").child(1).text()); } catch (IOException e) {     e.printStackTrace(); } return map;    }

驱动天空下载