作者felaray (法拉鱼)
看板RegExp
标题[心得] 重复抓特定html标签资料
时间Wed Jun 19 13:07:09 2013
前天开始研究RexExp,这东西令人恼羞.做了两天以後才弄好想要的东西
所以在此分享给大家.语法为C#
说明:html是用HttpWebRequest抓出来的网页资料,
目标是抓出<dt>...</dt>里面的资料
string pattern = @"<dt[^>]*?>(?<word>.*?)</dt>";
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
MatchCollection matches = regex.Matches(html);
int index = 0;
foreach (Match match in matches)
{
GroupCollection groups = match.Groups;
string x = groups["word"].Value.Trim();
if (x != "") //因为抓到不明空白,所以在此解决
Response.Write( x + "<BR>");
要条列序号的话就把++index加入上行
}
输出资料
1: absolute bolometric magnitude 绝对热星等
2: absolute zero 绝对零度,绝对零点
3: acceleration 加速度
4: acceleration of gravity 重力加速度
5: accretion 吸积
6: Achernar 水委一
7: achondrites 无球粒陨石
8: achromatic lens 消色差透镜
9: albedo 反照率
10: Alcaid 摇光
11: Alcor 辅、开阳伴星
12: Alcyone 昂宿六
Html原始码(节录)
<dt><b>absolute zero 绝对零度,绝对零点 </b></dt>
--
1F:推 signm :先补血03/15 22:27
2F:→ yamimk :这里又不能嘘是要补血什麽?03/15 22:28
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.229.239.158
※ 编辑: felaray 来自: 220.229.239.158 (06/19 13:09)
3F:推 s25g5d4:[^>]*?既然已用否定 就不要再下非贪婪 效能会减损 06/19 14:31
4F:→ felaray:好的 谢谢建议.目前来说是小资料所以还感受不到XD 06/19 14:54
5F:推 henry10423:如果你用C#在做网页解析时,建议可以用HtmlAgilityPars 08/19 21:19
6F:→ henry10423:er,这个东西挺强大的。不需要用Regex 08/19 21:19
7F:→ felaray:抱歉现在才看到回应 我後来的确是用那个解决XD 01/09 19:20