[原创] Java 天气预告 - htmlparser2.0 + httpclient4.0
程序员文章站
2022-07-13 12:52:20
...
Java控制台版 - 天气预告
前段时间,没事儿的时候,就写了一个查看天气预告的小程序,就当学习了!现在将源代码贴上供大家交流。
(程序没有经过优化,仅供大家参考)
(有时候没优化的程序可能更适合学习使用哟!哈哈哈!让大家见笑了啊!)
(其它源代码参见附件,附件中有所需的所有jar包)
主方法:
前段时间,没事儿的时候,就写了一个查看天气预告的小程序,就当学习了!现在将源代码贴上供大家交流。
(程序没有经过优化,仅供大家参考)
(有时候没优化的程序可能更适合学习使用哟!哈哈哈!让大家见笑了啊!)
(其它源代码参见附件,附件中有所需的所有jar包)
主方法:
import java.io.File; import java.util.Map; import net.sf.json.JSONObject; import org.apache.commons.io.FileUtils; /** * @author Michael Leo * @version 2010/08/12 */ public class Weather { private static final String ENCODE = "GB2312"; private static StringBuffer file_data = new StringBuffer(); private static String temp; public static void main(String[] args) throws Exception { // Weather forecast url String url = "http://www.weather.com.cn/html/weather/101070201.shtml"; // Live data url String liveDataHtml = "http://www.weather.com.cn/data/sk/101070201.html"; String liveJson = ParseUtils.getContent(liveDataHtml); // Get live data bean @SuppressWarnings("unchecked") Map<String, Map<String, String>> rootMap = JSONObject .fromObject(liveJson); Map<String, String> liveMap = rootMap.get("weatherinfo"); WeatherInfoBean wib = new WeatherInfoBean(); wib.setCity(liveMap.get("city")); wib.setTime(liveMap.get("time")); wib.setTemperature(liveMap.get("temp")); wib.setHumidity(liveMap.get("SD")); wib.setAtmosphericPressure(liveMap.get("AP")); wib.setWindDirection(liveMap.get("WD")); wib.setWindForce(liveMap.get("WS")); wib.setWindSpeed(liveMap.get("sm") + "m/s"); // Title temp = "================ " + wib.getCity() + "天气预报 ================"; System.out.println(temp); file_data.append(temp + "\r\n"); temp = "================ 中国气象局 ================\r\n"; System.out.println(temp); file_data.append(temp + "\r\n"); // Display live data StringBuffer sb = new StringBuffer(); temp = ParseUtils.getElementByTagName(url, "DT")[0]; System.out.println(temp); file_data.append(temp + "\r\n"); sb.append("最新实况: "); sb.append(wib.getCity()).append(" (").append(wib.getTime()).append(")"); System.out.println(sb.toString()); file_data.append(sb.toString() + "\r\n"); temp = "---------------------------------------------"; System.out.println(temp); file_data.append(temp + "\r\n"); sb = new StringBuffer(); sb.append("气温: ").append(wib.getTemperature()).append("℃\t"); sb.append("温度: ").append(wib.getHumidity()).append('\t'); sb.append("气压: ").append(wib.getAtmosphericPressure()); System.out.println(sb.toString()); file_data.append(sb.toString() + "\r\n"); sb = new StringBuffer(); sb.append("风向: ").append(wib.getWindDirection()).append('\t'); sb.append("风力: ").append(wib.getWindForce()).append('\t'); sb.append("风速: ").append(wib.getWindSpeed()); System.out.println(sb.toString()); file_data.append(sb.toString() + "\r\n"); temp = "---------------------------------------------\r\n"; System.out.println(temp); file_data.append(temp + "\r\n"); // ----------------------------------------------------- // Get weather forecast information String futureHtml = ParseUtils.getElementByAttr(url, "class", "weatherYubaoBox"); String tds = ParseUtils.getTableInfo(futureHtml, ENCODE); String[] tdArray = tds.split("\r\n"); for (int i = 0; i < tdArray.length; i++) { if (tdArray[i].contains("级")) { System.out.print(tdArray[i]); System.out.println(); file_data.append(tdArray[i] + "\r\n"); continue; } else if (tdArray[i].contains("星期")) { if (i != 0) { System.out .println("---------------------------------------------"); file_data .append("---------------------------------------------\r\n"); } System.out.print(tdArray[i]); System.out.println(); file_data.append(tdArray[i] + "\r\n"); continue; } System.out.print(tdArray[i]); System.out.print('\t'); file_data.append(tdArray[i] + "\t"); } FileUtils.writeStringToFile(new File(System.getProperty("user.dir") + "/weatherforecast.txt"), file_data.toString(), "UTF-8"); } }