读书人

高手帮忙用正则提取网页代码的一些信息

发布时间: 2011-12-31 23:50:30 作者: rapoo

高手帮忙用正则提取网页代码的一些信息,多谢……………………顶有分!!
前两天去面试,聊了一会
公司让我改一个网站来考察我的能力,昨天研究了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();

}

请高手多帮忙,谢谢各位了……

[解决办法]

[解决办法]
不会!顶!!!!狂顶!!!
[解决办法]
正则的用法是我短板,你可以研究一下,或者等熟悉的人写好给你
[解决办法]
Regex aRegex = new Regex(@ " <div id= " "map-layer-weaher " "> (? <weather> [^ <]*) </div> \s+ <div id= " "map-layer-temp " "> (? <temp> [^ <]*) </div> \s+ <div id= " "map-layer-wind " "> (? <wind> [^ <]*) </div> \s+ <div id= " "map-layer-uv " "> (? <uv> [^ <]*) </div> \s+ <div id= " "map-layer-ac " "> (? <ac> [^ <]*) </div> ");
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);
}
[解决办法]
这是lxcnn(过客)回的一个帖子,也许对你有用:
http://community.csdn.net/Expert/topic/5587/5587545.xml?temp=.9102899

正则表达式30分钟入门教程,我正在学习,有时间可以看看:
http://www.unibetter.com/deerchao/zhengzhe-biaodashi-jiaocheng-se.htm
[解决办法]
高手出现了!
[解决办法]
... 瞅瞅~
[解决办法]
帮顶
[解决办法]
using System.Text?
[解决办法]
正则搞不懂也不愿意搞,帮顶
[解决办法]
过客. 正则中的高!!!!!!!!!!!!!!!!!!
[解决办法]
来学习 顶一下
[解决办法]
string yourStr = ..........;
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掉
.$ ^ { [ ( | ) * + ? \
否则会抛异常,因为这些字符在正则中是有特殊意义的
[解决办法]
正则我是超级菜鸟级的,只能帮顶了

读书人网 >asp.net

热点推荐