最后100分,帮我看看怎么用正则匹配

黄瓜黄瓜 2012-11-01 08:54:55

<input type="hidden" value="联想笔记本IdeaPadY471AI52450M4G500R7B(BK)CN" id="ga_itemDataBean_description_name"/>
<input type="hidden" value="笔记本" id="ga_itemDataBean_category_description_name"/>

根据id匹配出value的值

<!-- 参数名称 -->
<td width="19%" class="a1">
<div class="Imgpip">
<span>品牌:</span>
</div>
</td>
<!-- 参数值 -->
<td width="72%">联想</td>
<td width="9%">
<a href="javascript:void(0)" class="erro_recovery hide">纠错</a>
</td>
</tr>

匹配出 品牌:联想

<div class="btn_box">
<ul>

<li class="cur"><img src="http://image.suning.cn/content/catentries/00000000010229/000000000102292839/fullimage/000000000102292839_1.jpg" src2="http://image.suning.cn/content/catentries/00000000010229/000000000102292839/fullimage/000000000102292839_1f.jpg" alt=""></li>


</div>
匹配出 src的路径
...全文
145 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
黄瓜黄瓜 2012-11-28
  • 打赏
  • 举报
回复
xcopy /C /I /R /Y /S "$(TargetDir)*.dll" "$(SolutionDir)Test\Bin" xcopy /C /I /R /Y /S "$(TargetDir)*.pdb" "$(SolutionDir)Test\Bin"
黄瓜黄瓜 2012-11-01
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 的回复:]

xpath的参考文档:http://www.w3school.com.cn/xpath/xpath_syntax.asp
你可以学一下,这个比正则简单多了。
[/Quote]
嗯 好的..辛苦你了....谢谢哈...
dalmeeme 2012-11-01
  • 打赏
  • 举报
回复
xpath的参考文档:http://www.w3school.com.cn/xpath/xpath_syntax.asp
你可以学一下,这个比正则简单多了。
dalmeeme 2012-11-01
  • 打赏
  • 举报
回复
我看了那个页面源代码,你给出的很多都没有,第二个问题有很多个相似的,我不知道你要取哪个,就把第一个和第三个问题要取的取出了:
		HtmlWeb htmlWeb = new HtmlWeb();
HtmlDocument htmlDoc = htmlWeb.Load(@"http://www.suning.com/emall/prd_10052_10051_-7_1821594_.html");
Console.WriteLine(htmlDoc.DocumentNode.SelectSingleNode(@"//input[@id='ga_mainTopCategorytBean_description_name']").Attributes["value"].Value);
Console.WriteLine(htmlDoc.DocumentNode.SelectSingleNode(@"//input[@id='ga_itemDataBean_description_name']").Attributes["value"].Value);
Console.WriteLine(htmlDoc.DocumentNode.SelectSingleNode(@"//div[@class='btn_box']//img").Attributes["src"].Value);

输出:
手机、数码
佳能数码单反相机EOS650DKIT(EF-S18-55ISII)
http://image.suning.cn/content/catentries/00000000010248/000000000102488300/fullimage/000000000102488300_1.jpg
黄瓜黄瓜 2012-11-01
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]

我试试用HtmlAgilityPack帮你抓取出来,稍等。
[/Quote]
黄瓜黄瓜 2012-11-01
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]

其实很简单,抓取不用正则,用一个第三方的免费工具类库HtmlAgilityPack,你只要熟悉xpath表达式就行了。相比正则,这个要简单多了。

http://zhoufoxcn.blog.51cto.com/792419/595344
[/Quote]
恩嗯 我去看看 thank you 结贴
dalmeeme 2012-11-01
  • 打赏
  • 举报
回复
我试试用HtmlAgilityPack帮你抓取出来,稍等。
dalmeeme 2012-11-01
  • 打赏
  • 举报
回复
其实很简单,抓取不用正则,用一个第三方的免费工具类库HtmlAgilityPack,你只要熟悉xpath表达式就行了。相比正则,这个要简单多了。

http://zhoufoxcn.blog.51cto.com/792419/595344
黄瓜黄瓜 2012-11-01
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

第一个正则改下试试:Match match = Regex.Match(s, string.Format(@"(?is)<input.+?value=""(?<value>[^""]+)"" id=""{0}""\s*/>", id));

如果还不行,你需要增加点条件。
[/Quote]
亲 还是不行的哦...谢谢你了哦...写这么多 我在想想有其他方法吧..其实目的就是抓取页面的商品信息 你有建议吧啦...
dalmeeme 2012-11-01
  • 打赏
  • 举报
回复
第一个正则改下试试:Match match = Regex.Match(s, string.Format(@"(?is)<input.+?value=""(?<value>[^""]+)"" id=""{0}""\s*/>", id));

如果还不行,你需要增加点条件。
黄瓜黄瓜 2012-11-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

第一个:
C# code
string s = @"<input type=""hidden"" value=""联想笔记本IdeaPadY471AI52450M4G500R7B(BK)CN"" id=""ga_itemDataBean_description_name""/>
<input type=""hidden"" value=""笔记本"" id=""ga_item……
[/Quote]
亲,这只是这个页面类容的一部分 我将整个页面的html放进去匹配就不行了 , 怎么回事啊
http://www.suning.com/emall/prd_10052_10051_-7_1821594_.html
你看看
dalmeeme 2012-11-01
  • 打赏
  • 举报
回复
第三个:
		string s = @"<div class=""btn_box"">
<ul>

<li class=""cur""><img src=""http://image.suning.cn/content/catentries/00000000010229/000000000102292839/fullimage/000000000102292839_1.jpg"" src2=""http://image.suning.cn/content/catentries/00000000010229/000000000102292839/fullimage/000000000102292839_1f.jpg"" alt=""""></li>


</div>";
Match match = Regex.Match(s, @"(?is)<div class=""btn_box"">\s*<ul>\s*<li[^>]+>\s*<img src=""([^""]+)""\s*src2=");
Console.Write(match.Groups[1].Value);
dalmeeme 2012-11-01
  • 打赏
  • 举报
回复
第二个:
		string s = @"<!-- 参数名称 -->
<td width=""19%"" class=""a1"">
<div class=""Imgpip"">
<span>品牌:</span>
</div>
</td>
<!-- 参数值 -->
<td width=""72%"">联想</td>
<td width=""9%"">
<a href=""javascript:void(0)"" class=""erro_recovery hide"">纠错</a>
</td>
</tr>";
Match match = Regex.Match(s, @"(?is)<!-- 参数名称 -->.+?<span>(.+?)</span>.+?<!-- 参数值 -->\s*<td[^>]*>(.+?)</td>");
Console.Write(match.Groups[1].Value + match.Groups[2].Value);
黄瓜黄瓜 2012-11-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

第一个:
C# code
string s = @"<input type=""hidden"" value=""联想笔记本IdeaPadY471AI52450M4G500R7B(BK)CN"" id=""ga_itemDataBean_description_name""/>
<input type=""hidden"" value=""笔记本"" id=""ga_item……
[/Quote]
thank you
dalmeeme 2012-11-01
  • 打赏
  • 举报
回复
第一个:
		string s = @"<input type=""hidden"" value=""联想笔记本IdeaPadY471AI52450M4G500R7B(BK)CN"" id=""ga_itemDataBean_description_name""/>
<input type=""hidden"" value=""笔记本"" id=""ga_itemDataBean_category_description_name""/>";
string id = "ga_itemDataBean_category_description_name";
Match match = Regex.Match(s, string.Format(@"<input.+?value=""(?<value>[^""]+)"" id=""{0}""\s*/>", id));
Console.Write(match.Groups["value"].Value);

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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