/* Reference:
http://jsoup.org/apidocs/org/jsoup/select/Selector.html
http://www.oschina.net/question/12_14127
http://wankunde.iteye.com/blog/1504824
*/
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Iterator;
import java.util.Date;
import java.util.Map;
import java.util.Map.Entry;
import java.text.SimpleDateFormat;
// jsoup class
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.jsoup.parser.XmlTreeBuilder;
import org.jsoup.parser.Parser;
public class parse_weather {
public static void main(String[] args)
{
if (args.length !=2)
{
System.out.println("Usage: parse_weather <city_name> <city_id>");
System.exit(-1);
}
String city_name=args[0];
String city_id=args[1];
parse_real_time_weather_info(city_name);
parse_7_days_weather_info(city_id);
parse_15_days_weather_info(city_id);
}
public static void parse_real_time_weather_info(String city_name)
{
String url = "http://flash.weather.com.cn/wmaps/xml/"+ city_name +".xml";
String cityname="";
String low_tem="";
String high_tem="";
String cur_weather="";
String wind_level="";
String wind_type="";
String humidity="";
String cur_time="";
String time_bind="";
try
{
Document doc = Jsoup.connect(url).get();
String body_text = doc.toString();
System.out.println("Weather Source:\n"+body_text);
Document xml_doc = Jsoup.parse(body_text, "", new Parser(new XmlTreeBuilder()));
/*
* <shenzhen dn="nay">
<city cityX="199" cityY="293" cityname="深圳市" centername="深圳市" fontColor="FFFF00" pyName="" state1="1" state2="1" stateDetailed="多云" tem1="21" tem2="25" temNow="21" windState="微风" windDir="东风" windPower="2级" humidity="81%" time="20:35" url="101280601"/>
</shenzhen>
*
*/
Elements city = xml_doc.select("city"); //带有city属性的全部元素集合
System.out.println("After parse, The weather information of ["+ city_name +"] is as below:");
// List<Map<String, String>> weather_infos = new ArrayList<Map<String, String>>();
Map<String, String> Map = new LinkedHashMap<String, String>();
for (Element sub_attr : city) {
if (sub_attr.hasAttr("cityname"))
{
cityname=sub_attr.attr("cityname");
Map.put("cityname", cityname);
}
if (sub_attr.hasAttr("tem1"))
{
low_tem=sub_attr.attr("tem1");
Map.put("low_tem", low_tem);
}
if (sub_attr.hasAttr("tem2"))
{
high_tem=sub_attr.attr("tem2");
Map.put("high_tem", high_tem);
}
if (sub_attr.hasAttr("statedetailed"))
{
cur_weather=sub_attr.attr("statedetailed");
Map.put("realtime_wea", cur_weather);
}
if (sub_attr.hasAttr("windstate"))
{
wind_level=sub_attr.attr("windstate");
Map.put("wind_level", wind_level);
}
if (sub_attr.hasAttr("winddir"))
{
wind_type=sub_attr.attr("winddir");
Map.put("wind_type", wind_type);
}
if (sub_attr.hasAttr("humidity"))
{
humidity=sub_attr.attr("humidity");
Map.put("humidity", humidity);
}
//last collection time
if (sub_attr.hasAttr("time"))
{
cur_time=sub_attr.attr("time");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date currentDate = new Date();
time_bind=sdf.format(currentDate)+" "+cur_time;
Map.put("collect_time", time_bind);
// weather_infos.add(Map);
}
// print keys,values from HashMap
Iterator<Entry<String, String>> iter = Map.entrySet().iterator();
while (iter.hasNext())
{
Map.Entry entry = (Map.Entry) iter.next();
Object key = entry.getKey();
Object val = entry.getValue();
System.out.println(entry.getKey()+":" + entry.getValue());
}
}
}
catch (IOException iex)
{
System.err.println(iex);
}
}
public static void parse_7_days_weather_info(String city_id)
{
// http://m.weather.com.cn/mweather/101010100.shtml
String url = "http://m.weather.com.cn/mweather/"+ city_id +".shtml";
String low_temp="";
String high_temp="";
String cur_temp="";
String arr_temp[];
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date currentDate = new Date();
String cur_date = sdf.format(currentDate);
try
{
Document doc = Jsoup.connect(url).get();
String body_text = doc.toString();
// System.out.println("Weather Source:\n"+body_text);
Elements temp_7days = doc.select("div#layout>div.days7>ul>li>span");
// List<List<Date>,Map<String, String>> weather_infos = new ArrayList<List<Date>,Map<String, String>>();
Map<String, String> Map = new LinkedHashMap<String, String>();
int jsq=1;
System.out.print("\n");
System.out.println("Below example will output the tempature information in the next 7 days...");
System.out.println("Start Date:"+cur_date);
for (Element sub_ele: temp_7days)
{
cur_temp = sub_ele.text();
// System.out.println("Tempature:"+cur_temp);
//Filter low temp and high temp
arr_temp=cur_temp.split("~");
// System.out.println("Array length of tempature:"+arr_temp.length);
if (arr_temp.length >1)
{
low_temp=arr_temp[0];
high_temp=arr_temp[1];
Map.put("Day "+jsq +"\'s highest/lowest temp","("+low_temp+","+high_temp+")");
}
else
{
Map.put("Day "+jsq +"\'s highest/lowest temp","("+cur_temp+")");
}
jsq++;
}
// print keys,values from HashMap
Iterator<Entry<String, String>> iter = Map.entrySet().iterator();
while (iter.hasNext())
{
Map.Entry entry = (Map.Entry) iter.next();
Object key = entry.getKey();
Object val = entry.getValue();
System.out.println(entry.getKey()+":" + entry.getValue());
}
}
catch (IOException iex)
{
System.err.println(iex);
}
}
public static void parse_15_days_weather_info(String city_id)
{
// http://m.weather.com.cn/mweather15d/101010100.shtml
String url = "http://m.weather.com.cn/mweather15d/"+ city_id +".shtml";
String low_temp="";
String high_temp="";
String wind_info="";
String ele_text="";
String title_text[];
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date currentDate = new Date();
String cur_date = sdf.format(currentDate);
// ArrayList<String> list=null;
try
{
Document doc = Jsoup.connect(url).get();
// String body_text = doc.toString();
// System.out.println("Weather Source:\n"+body_text);
Map<String, String> Map = new LinkedHashMap<String, String>();
Elements temp_15days_tb = doc.select("div#layout>div.yb15>table");
// list = new ArrayList<String>();
int jsq=1;
System.out.println("\nBelow example will output the temperature information in the next 15 days...");
System.out.println("Start Date: "+cur_date);
Elements title_ele = doc.select("div#layout>div.title");
title_text=title_ele.text().split(" ");
if (title_text.length >1)
{
System.out.println("City: "+ title_text[1]);
}
else
{
System.out.println("City: "+ title_text[0]);
}
for (Element ele: temp_15days_tb)
{
if(ele.text()!=null&& !"".equals(ele.text())){
Elements es = ele.s
评论0
最新资源