[原创] Java 天气预告 - htmlparser2.0 + httpclient4.0

Java控制台版 - 天气预告

    前段时间,没事儿的时候,就写了一个查看天气预告的小程序,就当学习了!现在将源代码贴上供大家交流。
(程序没有经过优化,仅供大家参考)
(有时候没优化的程序可能更适合学习使用哟!哈哈哈!让大家见笑了啊!)
(其它源代码参见附件,附件中有所需的所有jar包)
主方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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");
}
}

坚持原创及高品质技术分享,您的支持将鼓励我继续创作!