前两天去面试,聊了一会
公司让我改一个网站来考察我的能力,昨天研究了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" >天津&nbsp;&nbsp;&nbsp;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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<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();
        
    }请高手多帮忙,谢谢各位了……

解决方案 »

  1.   

    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);
    }
      

  2.   

    trystring yourStr = .........;
    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";    //风向
    }
      

  3.   

    这是lxcnn(过客)回的一个帖子,也许对你有用:
    http://community.csdn.net/Expert/topic/5587/5587545.xml?temp=.9102899正则表达式30分钟入门教程,我正在学习,有时间可以看看:
    http://www.unibetter.com/deerchao/zhengzhe-biaodashi-jiaocheng-se.htm
      

  4.   

    正在测试  0009(夏天以南) 提供的
    报错:非静态的字段、方法或属性“System.Text.RegularExpressions.Group.Success.get”要求对象引用
      

  5.   

    to lxcnn(过客) 
    能把那个页面中的所有城市信息都提出来,而不是天津的
      

  6.   

    那以什么为标识可以定位到天津部分,“<div id=天津”可以吗?这样试下
    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";    //风向
    }
      

  7.   

    ok lxcnn(过客) 的测试通过高手就是高手
      

  8.   

    还要麻烦 lxcnn(过客) 一下
    能不能把天津换成一个变量
    就是抓取不定城市的信息,北京或者上海等等
      

  9.   

    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掉
     .$ ^ { [ ( | ) * + ? \ 
    否则会抛异常,因为这些字符在正则中是有特殊意义的