通过百度获取天气预报
通过百度获取天气预报http://htmlparser.com.cn/post/20090917323.html
public class Getweather { /** * @param args * @throws ParserException */ public static void getWeather(String url) throws ParserException//通过百度获得天气预报, { Parser parser=new Parser("http://www.baidu.com/s?wd=%CC%EC%C6%F8");//URLDecoder码。代表天气自己转换就行 NodeFilter filter=new HasAttributeFilter("class","al_tr"); NodeList nodelist=parser.extractAllNodesThatMatch(filter); for(String a:nodelist.elementAt(0).toPlainTextString().trim().split(" ")) { if(!"".equals(a)) System.out.println(a); } } public static void getWeatherImage() throws ParserException//获得天气图片的链接URL { Parser parser=new Parser("http://www.baidu.com/s?wd=%CC%EC%C6%F8"); NodeFilter filter=new HasAttributeFilter("class","al_tr"); NodeList nodelist=parser.extractAllNodesThatMatch(filter); nodelist=nodelist.elementAt(0).getChildren(); NodeFilter filter1=new NodeClassFilter(ImageTag.class); nodelist=nodelist.extractAllNodesThatMatch(filter1,true); for(int i=0;i<nodelist.size();i++) { ImageTag image=(ImageTag) nodelist.elementAt(i); DownLoadImg(image.getImageURL(),String.valueOf(i)); } } public static void DownLoadImg(String url,String name)//下载对应的天气图片。 { HttpClient hc=new HttpClient(); GetMethod gm=new GetMethod(url); try { hc.executeMethod(gm); String path="/home/weather/"; File file=new File(path); if(!file.exists()) { file.mkdirs(); } String imagepath=path+name+".gif"; file=new File(imagepath); if(!file.exists()) { file.createNewFile(); } FileOutputStream out=new FileOutputStream(file); out.write(gm.getResponseBody()); out.close(); }catch (Exception e) { } } public static void main(String[] args) throws UnsupportedEncodingException, ParserException { getWeatherImage(); } }
页:
[1]