再做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的启动模式