242
技術社區[雲棲]
再做JAVA天氣預報
看了兩周的馬士兵J2SE視頻,參與了BOSS係統的需求,接下來可能沒有太多的時間學習馬士兵的J2SE了。目前已經學習到J2SE5.0第九章線程的基本概念、Sleep、Join、Yield、Priority部分了,接下來再學習就是線程同步部分了。網絡、GUI、專題日期處理、正則表達式也需要看。這隻能利用周末或者業餘的時候看了。今天是11月18日,還是爭取在12月15日之前看完這些。
馬士兵的JDBC連接數據庫部分的視頻也需要抽空看,做筆記,敲代碼。這個就放在12月15日-12月31日之間完成。總之這係列的東西年底需要全部看完。
今天頭給了一個任務,天氣預報的JAVA實現。大致需求是這樣的:
將中國天氣網https://www.weather.com.cn/的近十天的天氣數據抓取,可以抓取為XML,保存到本地;
對XML進行解析,將解析出城市、天氣、溫度、濕度、風向、風速等字段的信息保存在數據庫中;
細化一下,需要用到如下技術:
爬蟲抓取
解析XML(DOM,SAX,DOM4J等)
MYSQL數據庫的使用(設計)
編寫城市大循環算法,可以查找所有城市
增加根據IP查詢當前使用者城市的功能
定時器,每天自動更新數據庫信息
上次根據網上的代碼抄抄改改,已經完成了爬蟲抓取、解析XML、MYSQL數據字段的讀進。但是畢竟算改寫別人的代碼,學習了兩周J2SE之後,這次要完全寫出自己的程序。
給自己寫個時間規劃吧:
現在是2011年11月18日,周五,因為晚上6點要回去早點休息,周六周日阿狸要來北京玩兩天,明早5點半得起床去北京站接站。所以離下班隻有兩小時,今天下班前研究一下網頁文件的抓取,抓取保存到本地或者直接在控製台窗口輸出。有空的話再研究一下DOM解析和SAX解析。
今天是2011年11月21日,找到了中國天氣網的接口:
中國天氣網接口:
6天預報:https://m.weather.com.cn/data/101010100.html
實時預報:https://www.weather.com.cn/data/sk/101010100.html
其中101010100為查詢編碼具體查詢的時候替換此編碼就行了(下麵附編碼),編碼分3部分:
第一部分:10101為省級代碼
第二部分:01為市級代碼
第三部分:00為市級以下代碼;
以上接口直接將我所需要的數據解析為JSON格式了,也就是說我拿到數據之後就可以稍微裝模作樣的解析一下就塞入數據庫中了。
經過1小時的簡單整理,將所有的城市號羅列出來,放在CSDN資源區了。位置如下:
https://download.csdn.net/detail/opzoonzhuzhengke/3820632
接下來要做的工作就是數據庫設計兩張表,一張曆史表、一張實時表,將內容存入數據庫。此外,還需要編寫一個定時器的功能,實現定時抓取。
下午寫了一段代碼,抓取一個網頁上的JSON數據,並且顯示在控製台上,增加了亂碼處理(UTF-8):
package com.zzk.cn;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
/**
* @version 1.0.0
* @author zhuzhengke
*
*/
public class GetWeather {
public static void main(String[] args) {
GetInfo();
}
/**
* 獲取網頁信息
*/
public static void GetInfo() {
URL url = null;
URLConnection conn = null;
InputStream in = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
url = new URL("https://m.weather.com.cn/data/101010100.html");
conn = url.openConnection();
in = conn.getInputStream();
isr = new InputStreamReader(in, "UTF-8");
br = new BufferedReader(isr);
String line = "";
while (null != (line = br.readLine())) {
System.out.println(line);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != br) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
br = null;
try {
if (null != isr) {
isr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
isr = null;
try {
if (null != in) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
in = null;
}
}
}
2011年11月22日
今天在ECLIPSE項目下增加了一個weather_code.properties,抓取和JSON解析都實現了,貼下代碼,明天要設計數據庫,並且把屬性信息放到WEATHER包裝類中:
package com.zzk.cn;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;
import net.sf.json.JSONObject;
/**
*
* @author zhuzhengke
* @version 1.0.0
*
*/
class ReadProperties {
public String getProperties(String key) {
Properties prop = new Properties();
InputStream inputStream = this.getClass().getResourceAsStream("/weather_code.properties");
try {
prop.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
} finally {
//關閉
if (null != inputStream) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return prop.getProperty(key);
}
}
public class GetWeather {
static String info = "";
/*主函數*/
public static void main(String[] args) throws UnsupportedEncodingException {
GetInfo();
Json();
Properties prop = new Properties();
InputStream inputStream = new GetWeather().getClass().getResourceAsStream("/weather_code.properties");
try {
prop.load(inputStream);
System.out.println(prop.getProperty("101020100"));
Enumeration<?> en = prop.propertyNames();
while(en.hasMoreElements()) {
String obj = (String)en.nextElement();
System.out.println(obj);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//關閉資源
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Json解析
*/
public static void Json() {
//以下為JSON格式抓取實例
/**
* {"weatherinfo":{"city":"北京","city_en":"beijing","date_y":
* "2011年11月22日","date":"辛卯年","week":"星期二","fchh":"11",
* "cityid":"101010100"
* ,"temp1":"12℃~-2℃","temp2":"7℃~-3℃","temp3":"8℃~-1℃"
* ,"temp4":"12℃~3℃","temp5":"12℃~4℃",
* "temp6":"11℃~4℃","tempF1":"53.6℉~28.4℉"
* ,"tempF2":"44.6℉~26.6℉","tempF3"
* :"46.4℉~30.2℉","tempF4":"53.6℉~37.4℉",
* "tempF5":"53.6℉~39.2℉","tempF6"
* :"51.8℉~39.2℉","weather1":"多雲轉晴","weather2"
* :"晴","weather3":"晴","weather4":"晴轉多雲",
* "weather5":"多雲轉晴","weather6":"陰"
* ,"img1":"1","img2":"0","img3":"0","img4"
* :"99","img5":"0","img6":"99","img7":"0","img8":"1",
* "img9":"1","img10"
* :"0","img11":"2","img12":"99","img_single":"1","img_title1"
* :"多雲","img_title2":"晴","img_title3":"晴",
* "img_title4":"晴","img_title5"
* :"晴","img_title6":"晴","img_title7":"晴","img_title8"
* :"多雲","img_title9":"多雲","img_title10":"晴",
* "img_title11":"陰","img_title12"
* :"陰","img_title_single":"多雲","wind1":"北風4-5級"
* ,"wind2":"微風","wind3":"微風","wind4":"微風","wind5":
* "微風","wind6":"微風","fx1"
* :"北風","fx2":"北風","fl1":"4-5級","fl2":"小於3級","fl3"
* :"小於3級","fl4":"小於3級","fl5":"小於3級","fl6":"小於3級",
* "index":"涼","index_d":
* "天氣涼,建議著厚外套加毛衣等春秋服裝。體弱者宜著大衣、呢外套。因晝夜溫差較大,注意增減衣服。"
* ,"index48":"冷","index48_d":
* "天氣冷,建議著棉衣、皮夾克加羊毛衫等冬季服裝。年老體弱者宜著厚棉衣或冬大衣。","index_uv"
* :"弱","index48_uv":"中等","index_xc":"較適宜","index_tr":"很適宜",
* "index_co":"較舒適"
* ,"st1":"11","st2":"-4","st3":"4","st4":"0","st5":"8","st6"
* :"0","index_cl":"較不宜","index_ls":"基本適宜","index_ag": "極不易發"}}
*/
JSONObject jsonob = JSONObject.fromObject((JSONObject.fromObject(info)
.getString("weatherinfo")));
String city = jsonob.getString("city");// 城市
System.out.println(city);
String date_y = jsonob.getString("date_y");// 時間
System.out.println(date_y);
String date = jsonob.getString("date");// 農曆年
System.out.println(date);
String week = jsonob.getString("week");// 星期
System.out.println(week);
String cityid = jsonob.getString("cityid");// 城市號
System.out.println(cityid);
String temp1 = jsonob.getString("temp1");// 以下是六天內攝氏溫度
System.out.println(temp1);
String temp2 = jsonob.getString("temp2");
System.out.println(temp2);
String temp3 = jsonob.getString("temp3");
System.out.println(temp3);
String temp4 = jsonob.getString("temp4");
System.out.println(temp4);
String temp5 = jsonob.getString("temp5");
System.out.println(temp5);
String temp6 = jsonob.getString("temp6");
System.out.println(temp6);
String tempF1 = jsonob.getString("tempF1");// 以下是六天內華氏溫度
System.out.println(tempF1);
String tempF2 = jsonob.getString("tempF2");
System.out.println(tempF2);
String tempF3 = jsonob.getString("tempF3");
System.out.println(tempF3);
String tempF4 = jsonob.getString("tempF4");
System.out.println(tempF4);
String tempF5 = jsonob.getString("tempF5");
System.out.println(tempF5);
String tempF6 = jsonob.getString("tempF6");
System.out.println(tempF6);
String weather1 = jsonob.getString("weather1");// 以下是六天天氣
System.out.println(weather1);
String weather2 = jsonob.getString("weather2");
System.out.println(weather2);
String weather3 = jsonob.getString("weather3");
System.out.println(weather3);
String weather4 = jsonob.getString("weather4");
System.out.println(weather4);
String weather5 = jsonob.getString("weather5");
System.out.println(weather5);
String weather6 = jsonob.getString("weather6");
System.out.println(weather6);
String wind1 = jsonob.getString("wind1");// 以下六天為風力
System.out.println(wind1);
String wind2 = jsonob.getString("wind2");
System.out.println(wind2);
String wind3 = jsonob.getString("wind3");
System.out.println(wind3);
String wind4 = jsonob.getString("wind4");
System.out.println(wind4);
String wind5 = jsonob.getString("wind5");
System.out.println(wind5);
String wind6 = jsonob.getString("wind6");
System.out.println(wind6);
String fl1 = jsonob.getString("fl1");// 以下為六天風級
System.out.println(fl1);
String fl2 = jsonob.getString("fl2");
System.out.println(fl2);
String fl3 = jsonob.getString("fl3");
System.out.println(fl3);
String fl4 = jsonob.getString("fl4");
System.out.println(fl4);
String fl5 = jsonob.getString("fl5");
System.out.println(fl5);
String fl6 = jsonob.getString("fl6");
System.out.println(fl6);
String index_d = jsonob.getString("index_d");// 當日穿衣指數
System.out.println(index_d);
System.out.println();
// System.out.println("以下僅僅是一個Demo");
// 解析的數據格式2:{"classroom":"0801","peoples":[{"field1":"name1","field2":"age1"},{"field0":"name2","field2":"age2說"}]}
// info =
// "{\"classroom\":\"111\",\"peoples\":[{\"field1\":\"zzk1\",\"field2\":\"age1\"},{\"field1\":\"zzk2\",\"field2\":\"age2\"}]}";
//
// jsonob = JSONObject.fromObject(info);
//
// String classname = jsonob.getString("classroom");
// System.out.println(classname);
//
// JSONArray jsons = jsonob.getJSONArray("peoples");
// int jsonLength = jsons.size();
//
// // 對json數組進行循環
// for (int i = 0; i < jsonLength; i++) {
// JSONObject tempJson = JSONObject.fromObject(jsons.get(i));
//
// String name = StringEscapeUtils.escapeSql(tempJson
// .getString("field1"));
// String age = StringEscapeUtils.escapeSql(tempJson
// .getString("field2"));
//
// System.out.println(name + "-" + age);
// }
}
/**
* 獲取網頁信息
*/
public static void GetInfo() {
String id = "101020100";
String path = "https://m.weather.com.cn/data/"+id+".html";
URL url;
String inputline = "";
try {
url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10 * 1000);
conn.setRequestMethod("GET");
InputStreamReader inStream = new InputStreamReader(
conn.getInputStream(), "UTF-8");
BufferedReader buffer = new BufferedReader(inStream);
while ((inputline = buffer.readLine()) != null) {
info += inputline;
}
} catch (ProtocolException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
//關閉資源
}
}
/**
* 封裝weather類
*/
public class Weather {
private String city;// 城市名
private String date;// 日期:yyyy年MM月d日
private String lunarDate;// 農曆日期/當日有
private String week;// 星期
private String fcTime;// 預報時間:24製小時數/當日有
private String temperature;// 當日氣溫
private String weather;// 天氣
private String wind;// 風力
/**
*
* get和set方法
*/
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getLunarDate() {
return lunarDate;
}
public void setLunarDate(String lunarDate) {
this.lunarDate = lunarDate;
}
public String getWeek() {
return week;
}
public void setWeek(String week) {
this.week = week;
}
public String getFcTime() {
return fcTime;
}
public void setFcTime(String fcTime) {
this.fcTime = fcTime;
}
public String getTemperature() {
return temperature;
}
public void setTemperature(String temperature) {
this.temperature = temperature;
}
public String getWeather() {
return weather;
}
public void setWeather(String weather) {
this.weather = weather;
}
public String getWind() {
return wind;
}
public void setWind(String wind) {
this.wind = wind;
}
// 重寫toString方法
@Override
public String toString() {
return "Weather [city=" + city + ", date=" + date + ", lunarDate="
+ lunarDate + ", week=" + week + ", fcTime=" + fcTime
+ ", temperature=" + temperature + ", weather=" + weather
+ ", wind=" + wind + "]";
}
}
}
最後更新:2017-04-02 06:52:09
上一篇:
Java遞歸搜索指定文件夾下的匹配文件
下一篇:
DPI與像素的關係
CentOS6 安裝並破解confluence 6.4
《數據驅動安全:數據安全分析、可視化和儀表盤》一3.5 回到具體問題
iOS開發學習之路 No.10 :UIColor,CGColor,CIColor三者的區別和聯係
教你如何使用JSONP數據格式,如何使用jQuery
微服務架構實踐之郵件通知係統改造
E-MapReduce集群中HDFS服務集成Kerberos
點播和播放器下載需要的參數的區別(VideoId、AccessKeyId、AccessKeySecret、playKey、playauth)
混合雲VPC組網場景和方案分享(一)
工業生產領域機器視覺技術嵌入式應用趨勢
Android開發9——Activity的啟動模式