[心得] 重複抓特定html標籤資料
看板RegExp (正規表示式 Regular Expression)作者felaray (法拉魚)時間12年前 (2013/06/19 13:07)推噓2(2推 0噓 3→)留言5則, 3人參與討論串1/1
前天開始研究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>
--
推
03/15 22:27,
03/15 22:27
→
03/15 22:28,
03/15 22:28
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.229.239.158
※ 編輯: felaray 來自: 220.229.239.158 (06/19 13:09)
推
06/19 14:31, , 1F
06/19 14:31, 1F
→
06/19 14:54, , 2F
06/19 14:54, 2F
推
08/19 21:19, , 3F
08/19 21:19, 3F
→
08/19 21:19, , 4F
08/19 21:19, 4F
→
01/09 19:20, , 5F
01/09 19:20, 5F
RegExp 近期熱門文章
PTT數位生活區 即時熱門文章