//提供一段天气预报的源码,仅供参考,摘自网络
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows.Forms;namespace ReportWeatherDemo
{
class CityWeather
{
//-- 将此处的页面地址替换为相应的地址就行了,可以登陆 www.cma.gov.cn 查询到相应的城市
//string pageUrl = "http://www.cma.gov.cn/tqyb/weatherdetail/57496.html"; //鄂州
string pageUrl = "http://www.cma.gov.cn/tqyb/weatherdetail/58367.html"; //上海 //-- 用于存储抓取出来的地点的名称
string dd = string.Empty; //-- 天气状况
string tqgk = string.Empty; //-- 气温
string qw = string.Empty; //-- 风力/风向
string fxfl = string.Empty; public string GetWeatherInfo()
{
WebClient wc = new WebClient();
wc.Credentials = CredentialCache.DefaultCredentials; Byte[] bt = wc.DownloadData(@pageUrl); string result = Encoding.Default.GetString(bt); if (result.Length <= 0)
{
System.Windows.Forms.MessageBox.Show("未找到指定的城市天气信息!");
result = "";
} int pos = result.IndexOf("<div class=\"font01\">3天预报 </div>"); if (pos > 0)
result = "未找到该城市的天气预报!";
else
{
pos = result.IndexOf("<div class=\"font01\">3天预报"); result = result.Substring(pos, result.IndexOf("穿衣指数") - pos); pos = result.IndexOf(" ") + 18;
dd = result.Substring(pos, result.IndexOf("</") - pos); pos = result.IndexOf("天气概况");
result = result.Substring(pos, result.IndexOf("<!--<tr>") - pos); pos = result.IndexOf("<td width=\"75\"");
result = result.Substring(pos); pos = result.IndexOf(">");
tqgk = result.Substring(pos + 1, result.IndexOf("</td>") - pos - 1); result = result.Substring(result.IndexOf("气 温"));
pos = result.IndexOf("cn\">") + 4; qw = result.Substring(pos, result.IndexOf("℃</td>") - pos + 1); result = result.Substring(result.IndexOf("风向/风力</td>"));
result = result.Substring(result.IndexOf("<td")); pos = result.IndexOf("cn\">") + 4; fxfl = result.Substring(pos, result.IndexOf("</td") - pos); tqgk = "今日天气:" + tqgk.Replace(" ", "");
qw = qw.Replace(" ", ""); //qw = "最高温度:" + qw.Replace("/", " 最低温度:");
qw = "最高温度:" + qw.Replace("/", ",最低温度:");
fxfl = "风向/风力:" + fxfl.Replace(" ", ""); result = dd + "," + tqgk + "," + qw + "," + fxfl; } return result;
}
}
}
解决方案 »
- Winform中如何实现表格动态添加新行
- 关于dataset在内存中的周期的问题!
- 在线等,摄像头问题
- 在c#中如何用javascript写颜色的问题
- 找书
- 一个Process.StandardOutput.ReadToEnd()使用问题
- ASPNET用户权限,问题,在线等
- 请问如何判断一个驱动器是本地物理驱动器还是网络映射驱动器?
- Winform Webbrowser 后台截图问题
- 谁帮我解释下这个正则表达式
- 如何使winform软件中的文字实现链接?
- 比如说我加载这个界面时,listview中有全部要显示的值,但是我现在只想要符合我要求的值,我就要在文本框中输入编号1,哪么listview中的编号第一位为1的都留下,其余的消失,我该如何做?
最后一种方式速度是最慢的,但它是最直观的。
网址下的 例如 郑州颐顺轩餐饮有限公司
河南省郑州市未来路65号 - 0371-65932299
米兰庐西餐厅
郑州二七区北二七路106号和邦大厦南2楼 - 0371-6263355
.
.
.
取出来