网页嵌套滚动5天的天气预报 RT,给出思路或代码都可以 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我记得天气预报的话webservice有个接口可以获取 1. 有些小网站提供的webservers本身就不稳定不能长期使用。2. 还有一些网站限制访问次数。3. 再有就是花钱买服务。然后google了下。发现有个不错的。就是利用中央电视台的网站来抓取城市的天气预报。源地址:http://blog.163.com/linfeng_0212/blog/static/6222138200972664013833/代码就不copy了。自己去看。还有一种方式去google抓取天气信息需要这个jar包 httpclient。jarpackage mobi.tuitui.util; import java.io.IOException; import java.net.URLEncoder; import java.util.Date; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.params.HttpMethodParams; public class WeatherReport { /** * 发送请求到GOOGLE获取天气信息页面 * @param city * @return * @throws Exception */ @SuppressWarnings("deprecation") private static String getInputStream(String city) { try { HttpClient httpClient = new HttpClient(); String hostUrl = "http://www.google.cn/search?hl=zh-CN&q=tq%20" + URLEncoder.encode(city); GetMethod getMethod = new GetMethod(hostUrl); // 使用系统提供的默认的恢复策略 getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); try { // 执行getMethod int statusCode = httpClient.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + getMethod.getStatusLine()); } String result = getMethod.getResponseBodyAsString(); return result; } catch (HttpException e) { // 发生致命的异常,可能是协议不对或者返回的内容有问题 System.out.println("Please check your provided http address!"); e.printStackTrace(); } catch (IOException e) { // 发生网络异常 e.printStackTrace(); } finally { // 释放连接 getMethod.releaseConnection(); } } catch (Exception e) { e.printStackTrace(); return null; } return null; } /** * 将获取的包含天气页面的信息解析出相关的天气信息 * @param result * @identifier * @return */ public static String readText(String result, String identifier) { Pattern shopNumberPattern = Pattern.compile(identifier); Matcher shopNamMatcher = shopNumberPattern.matcher(result); if (shopNamMatcher.find()) return shopNamMatcher.group(1); return ""; } public static String getWeather(String city) { String result = readText(getInputStream(city), "<div style=\"padding:5px;float:left\">(.+?)</div></div>"); String weatherDetail = readText(result, "<div style=\"font-size:140%\">(.+?)%"); weatherDetail = weatherDetail.replaceAll("<div>", "").replaceAll("</div>", "").replaceAll("<br>", "").replace(":", ":"); return weatherDetail; } }因为大部分的小网站虽然提供了webservice接口 但是超过了一定的访问量就不给数据了。 用httpClient发送请求,它返回一个xml,再用dom4j解析一下,封装起来显示到前台我用的是这个 http://www.google.com/ig/api?hl=zh_cn&weather=dalian jsp页面内容提交方式有哪些? scriptlet 与JS问题,快来看呀! 请教一个jsp的异常问题: java.io.File如何释放对文件的引用? 使用tapestry时如何获得循环的次序号? 简略图片动态传输的问题 紧急求教!!! java定位打印 关于JS方面的问题,请问高手们帮帮忙哦!源代码发出来了 简单问题,分数挺多 struts2 + spring + mybatis,无法找到action,大家帮我看看. 刚学完SSH2想有几个问题问问大家 log4j 之 DailyRollingFileAppender 问题,为什么不是每天生产新的日志文件呢???
发现有个不错的。
就是利用中央电视台的网站来抓取城市的天气预报。源地址:
http://blog.163.com/linfeng_0212/blog/static/6222138200972664013833/代码就不copy了。自己去看。还有一种方式去google抓取天气信息
需要这个jar包 httpclient。jarpackage mobi.tuitui.util;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class WeatherReport {
/**
* 发送请求到GOOGLE获取天气信息页面
* @param city
* @return
* @throws Exception
*/
@SuppressWarnings("deprecation")
private static String getInputStream(String city) {
try {
HttpClient httpClient = new HttpClient();
String hostUrl = "http://www.google.cn/search?hl=zh-CN&q=tq%20" + URLEncoder.encode(city);
GetMethod getMethod = new GetMethod(hostUrl);
// 使用系统提供的默认的恢复策略
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
try {
// 执行getMethod
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + getMethod.getStatusLine());
}
String result = getMethod.getResponseBodyAsString();
return result;
} catch (HttpException e) {
// 发生致命的异常,可能是协议不对或者返回的内容有问题
System.out.println("Please check your provided http address!");
e.printStackTrace();
} catch (IOException e) {
// 发生网络异常
e.printStackTrace();
} finally {
// 释放连接
getMethod.releaseConnection();
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return null;
}
/**
* 将获取的包含天气页面的信息解析出相关的天气信息
* @param result
* @identifier
* @return
*/
public static String readText(String result, String identifier) {
Pattern shopNumberPattern = Pattern.compile(identifier);
Matcher shopNamMatcher = shopNumberPattern.matcher(result);
if (shopNamMatcher.find())
return shopNamMatcher.group(1);
return "";
}
public static String getWeather(String city) {
String result = readText(getInputStream(city), "<div style=\"padding:5px;float:left\">(.+?)</div></div>");
String weatherDetail = readText(result, "<div style=\"font-size:140%\">(.+?)%");
weatherDetail = weatherDetail.replaceAll("<div>", "").replaceAll("</div>", "").replaceAll("<br>", "").replace(":", ":");
return weatherDetail;
}
}因为大部分的小网站虽然提供了webservice接口 但是超过了一定的访问量就不给数据了。