package tos.app.query.tools.weather.domain;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class WeatherReport {
public WeatherReport() {
super();
}
private static String getSoapRequest(String city) {
StringBuilder sb = new StringBuilder();
sb
.append("<?xml version=/"1.0/" encoding=/"utf-8/"?>"
+ "<soap:Envelope xmlns:xsi=/"http://www.w3.org/2001/XMLSchema-instance/" "
+ "xmlns:xsd=/"http://www.w3.org/2001/XMLSchema/" "
+ "xmlns:soap=/"http://schemas.xmlsoap.org/soap/envelope//">"
+ "<soap:Body> <getWeatherbyCityName xmlns=/"http://WebXml.com.cn//">"
+ "<theCityName>" + city
+ "</theCityName> </getWeatherbyCityName>"
+ "</soap:Body></soap:Envelope>");
return sb.toString();
}
/**
* 用户把SOAP请求发送给服务器端,并返回服务器点返回的输入流
*
* @param city
* 用户输入的城市名称
* @return 服务器端返回的输入流,供客户端读取
* @throws Exception
*/
private static InputStream getSoapInputStream(String city) throws Exception {
try {
String soap = getSoapRequest(city);
if (soap == null) {
return null;
}
URL url = new URL(
"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx");
URLConnection conn = url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Length", Integer.toString(soap
.length()));
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setRequestProperty("SOAPAction",
"http://WebXml.com.cn/getWeatherbyCityName");
OutputStream os = conn.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
osw.write(soap);
osw.flush();
osw.close();
InputStream is = conn.getInputStream();
return is;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 对服务器端返回的XML进行解析
*
* @param city
* 用户输入的城市名称
* @return 字符串 用,分割
*/
public static String getWeather(String city) {
try {
Document doc;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream is = getSoapInputStream(city);
doc = db.parse(is);
NodeList nl = doc.getElementsByTagName("string");
StringBuffer sb = new StringBuffer();
for (int count = 0; count < nl.getLength(); count++) {
Node n = nl.item(count);
if(n.getFirstChild().getNodeValue().equals("查询结果为空!")) {
sb = new StringBuffer("#") ;
break ;
}
sb.append(n.getFirstChild().getNodeValue() + "#/n");
}
is.close();
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) throws Exception {
System.out.println(getWeather("长沙"));
System.out.println("po&oi".split("&").length) ;
System.out.println("##".split("#").length) ;
}
}
package tos.app.query.tools.weather.domain;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class WeatherServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8") ;
String city = request.getParameter("city") ;
String info = WeatherReport.getWeather(city) ;
request.setAttribute("info", info) ;
request.getRequestDispatcher("/weatherInfo.jsp").forward(request, response) ;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response) ;
}
}
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>城市天气信息</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<LINK rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/views/common/styles/style.css">
</head>
<body>
<%
String info = (String) request.getAttribute("info") ;
if(info.equals("#")) {
%>
<h1>该城市暂不被支持天气预报服务</h1>
<%
} else {
String[] infos = info.split("#") ;
%>
<table border="1">
<tr>
<td colspan="3">
<strong><font color="blue">72小时天气预报</font>
<font color="red">
<!-- 城市名称 -->
<%=infos[0]%> <%=infos[1]%>
</font>
<font size="-1">
<%=infos[4] %>发布
</font>
</strong>
</td>
</tr>
<tr align="center">
<td>
<table>
<tr align="center">
<!-- 日期 -->
<td>
<font size="-1">
<%=infos[6] %>
</font>
</td>
</tr>
<tr align="center">
<!-- 天气图标 -->
<td>
<img src="<%=request.getContextPath()%>/images/a_<%=infos[8]%>" />
<img src="<%=request.getContextPath()%>/images/a_<%=infos[9]%>" />
</td>
</tr>
<tr align="center">
<!-- 天气细则 -->
<td>
<font size="-1">
<%=infos[5] %><br>
<%=infos[7] %><br>
</font>
</td>
</tr>
</table>
</td>
<td>
<table align="center">
<tr align="center">
<td>
<!-- 第二天的时间 -->
<font size="-1">
<%=infos[13] %>
</font>
</td>
</tr>
<tr align="center">
<td>
<!-- 第二天的天气图标 -->
<img src="<%=request.getContextPath()%>/images/a_<%=infos[15]%>">
<img src="<%=request.getContextPath()%>/images/a_<%=infos[16]%>">
</td>
</tr>
<tr align="center">
<td>
<!-- 第二天的天气细则 -->
<font size="-1">
<%=infos[12] %><br>
<%=infos[14] %><br>
</font>
</td>
</tr>
</table>
</td>
<td>
<table align="center">
<tr align="center">
<td>
<!-- 第三天的天气时间 -->
<font size="-1">
<%=infos[18] %>
</font>
</td>
</tr>
<tr align="center">
<td>
<!-- 第三天的天气图标 -->
<img src="<%=request.getContextPath()%>/images/a_<%=infos[20]%>" />
<img src="<%=request.getContextPath()%>/images/a_<%=infos[21]%>" />
</td>
</tr>
<tr align="center">
<td>
<!-- 第三天的天气细则 -->
<font size="-1">
<%=infos[17] %><br>
<%=infos[19] %><br>
</font>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="3">
<!-- 居家指数 -->
<strong><font color="blue">温馨提醒</font></strong>
</td>
</tr>
<tr>
<td colspan="3">
<table>
<tr>
<td>
<img src="<%=request.getContextPath()%>/images/sun.gif" />
</td>
<td>
<font size="-1">
<%=infos[10] %>
</font>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="3">
<%
String str = "" ;
String s1 = "" ;
String s1Content = "" ;
String s2 = "" ;
String s2Content = "" ;
String s3 = "" ;
String s3Content = "" ;
String s4 = "" ;
String s4Content = "" ;
String s5 = "" ;
String s5Content = "" ;
String s6 = "" ;
String s6Content = "" ;
String s7 = "" ;
String s7Content = "" ;
try {
//这里开始进行具体详细测试
str = infos[11] ;
//穿衣指数
s1 = str.substring(str.indexOf("穿衣指数:"),str.indexOf("穿衣指数:")+4) ;
s1Content = str.substring(str.indexOf("穿衣指数:")+5,str.indexOf("感冒指数:")) ;
//感冒指数
s2 = str.substring(str.indexOf("感冒指数:"),str.indexOf("感冒指数:")+4) ;
s2Content = str.substring(str.indexOf("感冒指数:")+5,str.indexOf("晨练指数:")) ;
//晨练指数
s3 = str.substring(str.indexOf("晨练指数:"),str.indexOf("晨练指数:")+4) ;
s3Content = str.substring(str.indexOf("晨练指数:")+5,str.indexOf("交通指数:")) ;
//交通指数
s7 = str.substring(str.indexOf("交通指数:"),str.indexOf("交通指数:")+4) ;
s7Content = str.substring(str.indexOf("交通指数:")+5,str.indexOf("中暑指数:")) ;
//中暑指数
s4 = str.substring(str.indexOf("中暑指数:"),str.indexOf("中暑指数:")+4) ;
s4Content = str.substring(str.indexOf("中暑指数:")+5,str.indexOf("防晒指数:")) ;
//防晒指数
s5 = str.substring(str.indexOf("防晒指数:"),str.indexOf("防晒指数:")+4) ;
s5Content = str.substring(str.indexOf("防晒指数:")+5,str.indexOf("旅行指数:")) ;
//旅行指数
s6 = str.substring(str.indexOf("旅行指数:"),str.indexOf("旅行指数:")+4) ;
s6Content = str.substring(str.indexOf("旅行指数:")+5) ;
%>
<table>
<tr>
<td width="50">
<img src="<%=request.getContextPath()%>/images/zhishu_01.gif" />
</td>
<td>
<strong>
<font size="-1">
<%=s1 %>
</font>
</strong>
</td>
<td>
<font size="-1">
<%=s1Content %>
</font>
</td>
</tr>
<tr>
<td width="50">
<img src="<%=request.getContextPath()%>/images/zhishu_20.gif" />
</td>
<td>
<strong>
<font size="-1">
<%=s2 %>
</font>
</strong>
</td>
<td>
<font size="-1">
<%=s2Content %>
</font>
</td>
</tr>
<tr>
<td width="50">
<img src="<%=request.getContextPath()%>/images/zhishu_03.gif" />
</td>
<td>
<strong>
<font size="-1">
<%=s3 %>
</font>
</strong>
</td>
<td>
<font size="-1">
<%=s3Content %>
</font>
</td>
</tr>
<tr>
<td width="50">
<img src="<%=request.getContextPath()%>/images/zhishu_22.gif" />
</td>
<td>
<strong>
<font size="-1">
<%=s4 %>
</font>
</strong>
</td>
<td>
<font size="-1">
<%=s4Content %>
</font>
</td>
</tr>
<tr>
<td width="50">
<img src="<%=request.getContextPath()%>/images/zhishu_07.gif" />
</td>
<td>
<strong>
<font size="-1">
<%=s5 %>
</font>
</strong>
</td>
<td>
<font size="-1">
<%=s5Content %>
</font>
</td>
</tr>
<tr>
<td width="50">
<img src="<%=request.getContextPath()%>/images/zhishu_31.gif" />
</td>
<td>
<strong>
<font size="-1">
<%=s6 %>
</font>
</strong>
</td>
<td>
<font size="-1">
<%=s6Content %>
</font>
</td>
</tr>
<tr>
<td width="50">
<img src="<%=request.getContextPath()%>/images/zhishu_11.gif" />
</td>
<td>
<strong>
<font size="-1">
<%=s7 %>
</font>
</strong>
</td>
<td>
<font size="-1">
<%=s7Content %>
</font>
</td>
</tr>
</table>
</td>
</tr>
</table>
<%
} catch(Exception e) {
}
%>
<%
}
%>
</body>
</html>
分享到:
相关推荐
自己写的爬虫程序, 可以从网站抓取有用的信息供自己使用,这个程序抓取了天气信息,通过正则匹配实现。可供PHPer学习使用
这是一个用Python编写抓取天气预报的代码示例,用python写天气查询软件程序很简单。这段代码可以获取当地的天气和、任意城市的天气预报,原理是根据url找到网站截取相应的数据展现。 python抓取广州天气并分析 实例...
天气预报和图片预览功能(实例) 环境:VS2008(VS2005,2008均可打开) 图片预览,引用别人的!由于同项目就顺便发出! 天气预报:抓取HTML,分析!
实例031 抓取网站整页面 109 实例032 电子石英钟 113 实例033 图片自动播放 115 实例034 MP3播放器 118 实例035 播放FLV文件 121 实例036 Flash播放器 125 第4章 报表打印技术 实例037 自定义...
//获取天气 $url = 'http://m.weather.com.cn/data/'; $id = '101181101'; //焦作的代号 $data = file_get_contents($url . $id .'.html'); $obj=json_decode($data); echo $obj->weatherinfo->city.'...
主要介绍了python微信聊天机器人改进版,定时或触发抓取天气预报、励志语录等,向好友推送 ,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
实例031 抓取网站整页面 109 实例032 电子石英钟 113 实例033 图片自动播放 115 实例034 MP3播放器 118 实例035 播放FLV文件 121 实例036 Flash播放器 125 第4章 报表打印技术 实例037 自定义...
实例031 抓取网站整页面 109 实例032 电子石英钟 113 实例033 图片自动播放 115 实例034 MP3播放器 118 实例035 播放FLV文件 121 实例036 Flash播放器 125 第4章 报表打印技术 实例037 自定义横向或...
实例031 抓取网站整页面 109 实例032 电子石英钟 113 实例033 图片自动播放 115 实例034 MP3播放器 118 实例035 播放FLV文件 121 实例036 Flash播放器 125 第4章 报表打印技术 实例037 自定义横向或...
实例031 抓取网站整页面 109 实例032 电子石英钟 113 实例033 图片自动播放 115 实例034 MP3播放器 118 实例035 播放FLV文件 121 实例036 Flash播放器 125 第4章 报表打印技术 实例037 自定义横向或...
实例031 抓取网站整页面 109 实例032 电子石英钟 113 实例033 图片自动播放 115 实例034 MP3播放器 118 实例035 播放FLV文件 121 实例036 Flash播放器 125 第4章 报表打印技术 实例037 自定义横向或...
实例031 抓取网站整页面 109 实例032 电子石英钟 113 实例033 图片自动播放 115 实例034 MP3播放器 118 实例035 播放FLV文件 121 实例036 Flash播放器 125 第4章 报表打印技术 实例037 ...
实例031 抓取网站整页面 109 实例032 电子石英钟 113 实例033 图片自动播放 115 实例034 MP3播放器 118 实例035 播放FLV文件 121 实例036 Flash播放器 125 第4章 报表打印技术 实例037 自定义横向或...
实例031 抓取网站整页面 109 实例032 电子石英钟 113 实例033 图片自动播放 115 实例034 MP3播放器 118 实例035 播放FLV文件 121 实例036 Flash播放器 125 第4章 报表打印技术 实例037 自定义...
实例031 抓取网站整页面 109 实例032 电子石英钟 113 实例033 图片自动播放 115 实例034 MP3播放器 118 实例035 播放FLV文件 121 实例036 Flash播放器 125 第4章 报表打印技术 实例037 自定义...
实例031 抓取网站整页面 109 实例032 电子石英钟 113 实例033 图片自动播放 115 实例034 MP3播放器 118 实例035 播放FLV文件 121 实例036 Flash播放器 125 第4章 报表打印技术 实例037 自定义...
实例031 抓取网站整页面 109 实例032 电子石英钟 113 实例033 图片自动播放 115 实例034 MP3播放器 118 实例035 播放FLV文件 121 实例036 Flash播放器 125 第4章 报表打印技术 实例037 自定义...
实例031 抓取网站整页面 109 实例032 电子石英钟 113 实例033 图片自动播放 115 实例034 MP3播放器 118 实例035 播放FLV文件 121 实例036 Flash播放器 125 第4章 报表打印技术 实例037 自定义...
实例031 抓取网站整页面 109 实例032 电子石英钟 113 实例033 图片自动播放 115 实例034 MP3播放器 118 实例035 播放FLV文件 121 实例036 Flash播放器 125 第4章 报表打印技术 实例037 自定义...
为方便mm看天气预报,随手给我她写的实用的天气预报小贴士。这是项目源代码,总共代码行数也就几十行,有兴趣的同志可以看一看