<div class="tqshow1"><h3>哈尔滨<font color="#0066cc">今日</font>天气</h3><p>星期二</p><ul><li class="tqpng"><img class='pngtqico' align='absmiddle' src='http://img.tianqi.com/static/images/tianqibig/b4.png' style='border:0;width:46px;height:46px'/></li><li><font color="#f00">29℃</font>~<font color="#4899be">20℃</font></li><li>雷阵雨</li><li style="height:18px;overflow:hidden">南风微风 </li></ul></div>
关键是要找到规律,比如说<font color="#f00">
这个是不是一定出现,并且仅仅在温度前出现,有没有别的温度。
但是更推荐的做法是,直接有天气预报的web service,这种从网站上抓数据不但麻烦,而且一旦网站改版就又得重写。
http://download.csdn.net/detail/clyszu/5480877
http://zhangkui.blog.51cto.com/1796259/497324/
http://download.csdn.net/detail/xiongaixin/892244
http://download.csdn.net/download/clyszu/5480877
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
String s = "<div class=\"tqshow1\">...\">29℃</font>~<font color=\"#4899be\">20℃...";
Regex reg = new Regex("([0-9]+)℃");
MatchCollection m = reg.Matches(s);
Console.WriteLine(m[0].Groups[1].Value);
Console.ReadLine();
}
}
}
输出
29