正则网页截取,急!急!急!

LGP88864237 2011-07-05 07:04:24


<!--day 1-->
<table class="yuBaoTable" width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="15%" rowspan="2" class="t0" style="background:#f4f7fc;"><a title="农历六月初一">1日星期五</a></td>
<td width="12%" style="background:#f4f7fc;">白天</td>
<td width="10%">
<a href="http://www.weather.com.cn/static/html/legend.shtml" target="_blank">
<img src="/m2/i/icon_weather/29x20/d00.gif" />
</a>
</td>
<td width="18%"><a href ="http://baike.weather.com.cn/index.php?doc-view-767.php" target="_blank">晴</a></td>
<td width="15%">
<a href="http://baike.weather.com.cn/index.php?doc-view-1148.php" target="_blank">
<b>高温 <strong>34<strong>℃</b>
</a>
</td>
<td width="15%">
<a href="http://baike.weather.com.cn/index.php?doc-view-778.php" target="_blank">
无持续风向
</a>
</td>
<td width="15%">
<a href="http://baike.weather.com.cn/index.php?doc-view-778.php" target="_blank">
微风
</a>
</td>
</tr>
<tr>
<td style="background:#f4f7fc;">夜间</td>
<td>
<a href="http://www.weather.com.cn/static/html/legend.shtml" target="_blank">
<img src="/m2/i/icon_weather/29x20/n01.gif" />
</a>
</td>
<td><a href ="http://baike.weather.com.cn/index.php?doc-view-769.php" target="_blank">多云</a></td>
<td>
<a href="http://baike.weather.com.cn/index.php?doc-view-1386.php" target="_blank">
<span>低温 <strong>26<strong>℃</span>
</a>
</td>
<td>
<a href="http://baike.weather.com.cn/index.php?doc-view-778.php" target="_blank">
无持续风向
</a>
</td>
<td>
<a href="http://baike.weather.com.cn/index.php?doc-view-778.php" target="_blank">
微风
</a>
</td>
</tr>
</table>
<!--day 2-->



截取之间的内容放入一个string[]里面,我的方法

public string[] htmlCrwal4(string htmlstr, string begstr, string endstr)
{


string temp = "";

int start, stop;

// start = HTML.IndexOf("</style>", 0, HTML.Length);``
start = htmlstr.IndexOf(begstr, 0, htmlstr.Length);

stop = htmlstr.IndexOf(endstr, 0, htmlstr.Length);
temp = htmlstr.Substring(start, stop - start);
Regex regex = new Regex("<[^>]*>");
string[] ss = regex.Split(temp);
string t = "";
foreach (string p in ss)
if (p.Trim().Length > 0)
t += p + "/";
string[] sss = t.Split('/');

return sss;

}


htmlCrwal4(htmlstring,"<!--day 1-->","<!--day 2-->"),



但是就是不对,是什么地方错了呢,或者用正则表达式能解决也可以,请大家支招,感谢了。
...全文
133 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
天蛾糊 2011-07-06
  • 打赏
  • 举报
回复
不是其所云
LGP88864237 2011-07-06
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 q107770540 的回复:]
C# code


void Main()
{
string html = @"<!--day 1-->
<table class=""yuBaoTable"" width=""100%"" border=""0"" cellspacing=""0"" cellpadding=""0"">
<tr>
<td w……
[/Quote]

您这只是把网页符号都去掉,我这个是整个网页的一部分,我取不完整。
诺维斯基 2011-07-06
  • 打赏
  • 举报
回复
顶一下!!
mbh0210 2011-07-05
  • 打赏
  • 举报
回复
你想取出啥内容了?
q107770540 2011-07-05
  • 打赏
  • 举报
回复

void Main()
{
string html = @"<!--day 1-->
<table class=""yuBaoTable"" width=""100%"" border=""0"" cellspacing=""0"" cellpadding=""0"">
<tr>
<td width=""15%"" rowspan=""2"" class=""t0"" style=""background:#f4f7fc;""><a title=""农历六月初一"">1日星期五</a></td>
<td width=""12%"" style=""background:#f4f7fc;"">白天</td>
<td width=""10%"">
<a href=""http://www.weather.com.cn/static/html/legend.shtml"" target=""_blank"">
<img src=""/m2/i/icon_weather/29x20/d00.gif"" />
</a>
</td>
<td width=""18%""><a href =""http://baike.weather.com.cn/index.php?doc-view-767.php"" target=""_blank"">晴</a></td>
<td width=""15%"">
<a href=""http://baike.weather.com.cn/index.php?doc-view-1148.php"" target=""_blank"">
<b>高温 <strong>34<strong>℃</b>
</a>
</td>
<td width=""15%"">
<a href=""http://baike.weather.com.cn/index.php?doc-view-778.php"" target=""_blank"">
无持续风向
</a>
</td>
<td width=""15%"">
<a href=""http://baike.weather.com.cn/index.php?doc-view-778.php"" target=""_blank"">
微风
</a>
</td>
</tr>
<tr>
<td style=""background:#f4f7fc;"">夜间</td>
<td>
<a href=""http://www.weather.com.cn/static/html/legend.shtml"" target=""_blank"">
<img src=""/m2/i/icon_weather/29x20/n01.gif"" />
</a>
</td>
<td><a href =""http://baike.weather.com.cn/index.php?doc-view-769.php"" target=""_blank"">多云</a></td>
<td>
<a href=""http://baike.weather.com.cn/index.php?doc-view-1386.php"" target=""_blank"">
<span>低温 <strong>26<strong>℃</span>
</a>
</td>
<td>
<a href=""http://baike.weather.com.cn/index.php?doc-view-778.php"" target=""_blank"">
无持续风向
</a>
</td>
<td>
<a href=""http://baike.weather.com.cn/index.php?doc-view-778.php"" target=""_blank"">
微风
</a>
</td>
</tr>
</table>
<!--day 2-->";

string[] result= Regex.Replace(html,"<[^>]*>","/").Split(new char[]{'/'},StringSplitOptions.RemoveEmptyEntries).Where(s=>!string.IsNullOrEmpty(s.Trim())).ToArray();
result.ToList().ForEach(s=>Console.WriteLine(s.Trim()));
/*
1日星期五
白天

高温
34

无持续风向
微风
夜间
多云
低温
26

无持续风向
微风

*/
}
q107770540 2011-07-05
  • 打赏
  • 举报
回复
你要截取神马? 能说的清楚点么

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧