前两天去面试,聊了一会
公司让我改一个网站来考察我的能力,昨天研究了2个小时,思路不是很清晰,所以请高手帮帮忙。问题描述:我要改的这个网站是在某机关的内部运行的,放在一个服务器上,服务器能上网,其他的客户端都不能上网,只能浏览架设在这个服务器上的这个网站。而这个网站要有天气预报的功能,现在他们是调用的新浪的天气预报,让我改成调用中国气象局的,因为中国气象局不提供相关的服务,因此只能用正则表达式进行抓取。
就是下面这个网页:http://www.cma.gov.cn/tqyb/weatherdetail/54517.html
我现在已经能读取到这个页面的html源码了,关键是如何用这则表达式提取需要的信息。所以请高手帮帮忙,小弟在此谢过!提取这段中的主要信息“<div id=天津 style="position:absolute; left:374px; top:200px; width:248px; height:202px; background-color:#3366CC; border:#fff 1px;z-index:1; visibility: hidden;">
<div align="left">
<div id="map-layer-city" >天津 6月12日</div>
<div id="map-layer-line" ></div>
<div id="map-layer-content" >
<div id="map-layer-pic"><img src="/tqyb/img/weather/a60x60gif/a2.gif"> <img src="/tqyb/img/weather/a60x60gif/a1.gif"></div>
<div id="map-layer-weaher">阴转多云</div>
<div id="map-layer-temp">32 ~ 24 ℃</div>
<div id="map-layer-wind">东南风3-4级转<=3级</div>
<div id="map-layer-uv">紫外线:弱</div>
<div id="map-layer-ac">空气质量:中</div>
</div>
</div>
</div>”需要提取:时间=6月12日,温度=32 ~ 24 ℃,风向东南风3-4级转<=3级,天气=阴转多云。------------我在网上找的代码,有问题,请高手指点---------
protected void Page_Load(object sender, EventArgs e)
{ string url = "http://www.cma.gov.cn/tqyb/weatherdetail/54517.html"; //获取输入的网页地址
WebRequest wreq = WebRequest.Create(url);
HttpWebResponse wresp = (HttpWebResponse)wreq.GetResponse();
string HTML = "";
Stream s = wresp.GetResponseStream();
StreamReader objReader = new StreamReader(s, System.Text.Encoding.GetEncoding("GB2312"));
string sLine = "";
int i = 0;
while (sLine != null)
{
i++;
sLine = objReader.ReadLine();
if (sLine != null)
HTML += sLine;
} String temp = "";
Regex aRegex = new Regex();//这个地方怎么写???
MatchCollection mc = aRegex.Matches(HTML);
foreach (Match m in mc)
{
temp+=m.Groups[0].Value;
}
Message.Text = temp.ToString();
}请高手多帮忙,谢谢各位了……
公司让我改一个网站来考察我的能力,昨天研究了2个小时,思路不是很清晰,所以请高手帮帮忙。问题描述:我要改的这个网站是在某机关的内部运行的,放在一个服务器上,服务器能上网,其他的客户端都不能上网,只能浏览架设在这个服务器上的这个网站。而这个网站要有天气预报的功能,现在他们是调用的新浪的天气预报,让我改成调用中国气象局的,因为中国气象局不提供相关的服务,因此只能用正则表达式进行抓取。
就是下面这个网页:http://www.cma.gov.cn/tqyb/weatherdetail/54517.html
我现在已经能读取到这个页面的html源码了,关键是如何用这则表达式提取需要的信息。所以请高手帮帮忙,小弟在此谢过!提取这段中的主要信息“<div id=天津 style="position:absolute; left:374px; top:200px; width:248px; height:202px; background-color:#3366CC; border:#fff 1px;z-index:1; visibility: hidden;">
<div align="left">
<div id="map-layer-city" >天津 6月12日</div>
<div id="map-layer-line" ></div>
<div id="map-layer-content" >
<div id="map-layer-pic"><img src="/tqyb/img/weather/a60x60gif/a2.gif"> <img src="/tqyb/img/weather/a60x60gif/a1.gif"></div>
<div id="map-layer-weaher">阴转多云</div>
<div id="map-layer-temp">32 ~ 24 ℃</div>
<div id="map-layer-wind">东南风3-4级转<=3级</div>
<div id="map-layer-uv">紫外线:弱</div>
<div id="map-layer-ac">空气质量:中</div>
</div>
</div>
</div>”需要提取:时间=6月12日,温度=32 ~ 24 ℃,风向东南风3-4级转<=3级,天气=阴转多云。------------我在网上找的代码,有问题,请高手指点---------
protected void Page_Load(object sender, EventArgs e)
{ string url = "http://www.cma.gov.cn/tqyb/weatherdetail/54517.html"; //获取输入的网页地址
WebRequest wreq = WebRequest.Create(url);
HttpWebResponse wresp = (HttpWebResponse)wreq.GetResponse();
string HTML = "";
Stream s = wresp.GetResponseStream();
StreamReader objReader = new StreamReader(s, System.Text.Encoding.GetEncoding("GB2312"));
string sLine = "";
int i = 0;
while (sLine != null)
{
i++;
sLine = objReader.ReadLine();
if (sLine != null)
HTML += sLine;
} String temp = "";
Regex aRegex = new Regex();//这个地方怎么写???
MatchCollection mc = aRegex.Matches(HTML);
foreach (Match m in mc)
{
temp+=m.Groups[0].Value;
}
Message.Text = temp.ToString();
}请高手多帮忙,谢谢各位了……
解决方案 »
- 对登录的密码进行加密
- 高手快来啊!
- 索引超出范围。必须为非负值并小于集合大小。 参数名: index --之前的代码有误,现在更改了一处,大家帮忙看看
- DetailView在单击编辑后如何取值??
- 关于DropDownList添加项
- 招聘兼职ASP.NET C#方向程序员 开发数据库系统
- 用户控件怎么保证登录后状态在其他页中保持不变呢,我想在左侧放入用户形象图片和用户名,急急急!!
- 如何实现类似Google Map的功能
- AspNetPager分页使用问题,比如我现在在第二页,当我点击修改或者删除是就跳转到到第一页了?
- 如何以数组的元素作为索引获取到该行数组的数据
- www.7735.com.cn这个网站是真的静态页吗?
- 问关闭窗口时如何用JSP脚本清空session
Match m = aRegex.Match(HTML);
if(Match.Success)
{
Console.WriteLine(m.Group["weather"].Value);
Console.WriteLine(m.Group["temp"].Value);
Console.WriteLine(m.Group["wind"].Value);
Console.WriteLine(m.Group["uv"].Value);
Console.WriteLine(m.Group["ac"].Value);
}
MatchCollection mc = Regex.Matches(yourStr, @"<div\s+id=""map-layer-city""\s*>[^<]*(?<time>\d+月\d+日)</div>[\s\S]*?<div\s+id=""map-layer-weaher"">(?<weaher>[^<]*)</div>\s*<div\s+id=""map-layer-temp"">(?<temp>[^<]*)</div>\s*<div\s+id=""map-layer-wind"">(?<wind>[\s\S]*?)</div>", RegexOptions.IgnoreCase);
foreach(Match m in mc)
{
richTextBox2.Text += m.Groups["time"].Value + "\n"; //时间
richTextBox2.Text += m.Groups["weaher"].Value + "\n"; //天气
richTextBox2.Text += m.Groups["temp"].Value + "\n"; //温度
richTextBox2.Text += m.Groups["wind"].Value + "\n"; //风向
}
http://community.csdn.net/Expert/topic/5587/5587545.xml?temp=.9102899正则表达式30分钟入门教程,我正在学习,有时间可以看看:
http://www.unibetter.com/deerchao/zhengzhe-biaodashi-jiaocheng-se.htm
报错:非静态的字段、方法或属性“System.Text.RegularExpressions.Group.Success.get”要求对象引用
能把那个页面中的所有城市信息都提出来,而不是天津的
string yourStr = .............;
Match m = Regex.Match(yourStr, @"<div\s+id=天津[\s\S]*?<div\s+id=""map-layer-city""\s*>[^<]*(?<time>\d+月\d+日)</div>[\s\S]*?<div\s+id=""map-layer-weaher"">(?<weaher>[^<]*)</div>\s*<div\s+id=""map-layer-temp"">(?<temp>[^<]*)</div>\s*<div\s+id=""map-layer-wind"">(?<wind>[\s\S]*?)</div>", RegexOptions.IgnoreCase);
if(m.Success)
{
richTextBox2.Text += m.Groups["time"].Value + "\n"; //时间
richTextBox2.Text += m.Groups["weaher"].Value + "\n"; //天气
richTextBox2.Text += m.Groups["temp"].Value + "\n"; //温度
richTextBox2.Text += m.Groups["wind"].Value + "\n"; //风向
}
能不能把天津换成一个变量
就是抓取不定城市的信息,北京或者上海等等
string city = textBox1.Text;
Match m = Regex.Match(yourStr, @"<div\s+id="+city+@"[\s\S]*?<div\s+id=""map-layer-city""\s*>[^<]*(?<time>\d+月\d+日)</div>[\s\S]*?<div\s+id=""map-layer-weaher"">(?<weaher>[^<]*)</div>\s*<div\s+id=""map-layer-temp"">(?<temp>[^<]*)</div>\s*<div\s+id=""map-layer-wind"">(?<wind>[\s\S]*?)</div>", RegexOptions.IgnoreCase);
if(m.Success)
{
richTextBox2.Text += m.Groups["time"].Value + "\n"; //时间
richTextBox2.Text += m.Groups["weaher"].Value + "\n"; //天气
richTextBox2.Text += m.Groups["temp"].Value + "\n"; //温度
richTextBox2.Text += m.Groups["wind"].Value + "\n"; //风向
}不过这里你要先做下预处理,就是如果用户输入了如下字符之一时,你要先把它Replace掉
.$ ^ { [ ( | ) * + ? \
否则会抛异常,因为这些字符在正则中是有特殊意义的