日期:2014-05-16  浏览次数:20403 次

Java通过jsoup实现网页天气数据解析

?

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;


public class WeatherTest {

	/**Java通过jsoup实现网页天气数据解析
	 * @param args
	 * @throws Exception 
	 * @throws UnsupportedEncodingException 
	 * @throws MalformedURLException 
	 */
	public static void main(String[] args) throws MalformedURLException, UnsupportedEncodingException, Exception {
		Document doc = getURLContent("http://www.soso.com/tb.q?cid=webq.wea");
		String content = doc.html();
		int beginLocal = content.indexOf("<xml id=\"WeatherData\" style=\"display:none\"> ");
		int endLocal = content.indexOf("</xml>");
		content = content.substring(beginLocal, endLocal);
		
		String cityStr = getXMLVarByName("<city>","</city>",content);
		System.out.println("城市:"+cityStr);
		
		String dateStr = getXMLVarByName("<date>","</date>",content);
		System.out.println("日期:"+dateStr);
		
		String todayTemperature = getXMLVarByName("<temperature>","</temperature>",content);
		System.out.println("今日气温:"+todayTemperature);
		
		String todayWeather = getXMLVarByName("<weather>","</weather>",content);
		System.out.println("今日天气:"+todayWeather);
		
		String todayWind = getXMLVarByName("<wind>","</wind>",content);
		System.out.println("今日风向:"+todayWind);
	}
	/**
	 * 获取xml格式的信息
	 * @param name
	 * @param content
	 * @return
	 */
	private static String getXMLVarByName(String startStr, String endStr,String content){
		String xmlData = content;		
		int begin = xmlData.indexOf(startStr);		
		int end = xmlData.indexOf(endStr);		
		String result=xmlData.substring(begin+startStr.length(),end);
		result = result.trim();		
		return result;
	}
/**
 * 获取网页
 * @param docUrl
 * @return
 * @throws MalformedURLException
 * @throws IOException
 * @throws UnsupportedEncodingException
 */
	private static Document getURLContent(String docUrl) throws MalformedURLException, IOException, UnsupportedEncodingException {
		Document doc = Jsoup.connect(docUrl)
		  .data("query", "Java")
		  .userAgent("Mozilla")
		  .cookie("auth", "token")
		  .timeout(3000)
		  .post();
		return doc;
	}
}

?需要jsoup的jar包,在附件上。

相关资料更多>