看板 Perl 關於我們 聯絡資訊
※ 引述《b60413 (None)》之銘言: : 關於這個問題 我知道可以用正規式直接去實做 : 但是對於正規式的使用還沒有很透徹的了解 : 想問看看是否有相關的HTML module可以把html tag消除掉 : 只留下tag跟tag中的內容 : (有到cpan網站中去找 但是資料過於龐大 找得很沒頭緒) : 或者有人可以跟我講 如何利用正規式去實做嗎? : 有查到可用<(.| )*?>去消除所有的HTML Tag : 但是消除後的文字Home與Test連在一起....希望能做到分開的效果 : 或者是結果儲存在一個陣列當中 : 謝謝 : HTML Example: : <a href="http://127.0.0.1">Home<span>Test</span></a> : 須將Home跟Test抓出來 #------------------------------------------- use HTML::Strip; open(INPUT, "c:/a.txt"); @temp = <INPUT>; chomp @temp; foreach $t (@temp) { my $hs = HTML::Strip->new(); my $clean_text = $hs->parse($t); $hs->eof; print "$clean_text"; } 結果:Home Test #------------------------------------------- open(input, "c:/a.txt") or die; @temp = <input>; chomp(@temp); foreach (@temp) { my $tree = HTML::TreeBuilder->new; $tree->parse($_); my $formatter = HTML::FormatText->new; my $string = $formatter->format($tree); print $string; } 結果:HomeTest #------------------------------------------- 用過這兩個模組,第一個清的較乾淨 <a href="http://127.0.0.1">Home<span>Test</span></a> 存成變數用兩個模組清, 出來結果還是有tag,不太知道為何會這樣===>自己寫rgexp抓要的data... -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.223.225.6 ※ 編輯: deh3215 來自: 140.117.168.75 (04/03 09:13)
b60413:感謝deh3215 ~ 我在自己試看看 04/03 22:57
deh3215:我是用第2個模組清除所有tag..搜尋"html",應該會有你要的 04/03 23:07
deh3215:答案...要自己寫rgexp抓..用模組也是可以,抓特定tagy 04/03 23:09
deh3215:剛在test一下..兩個模組多少都會有點遺漏tag..須自己手動 04/03 23:12
deh3215:清除.. 04/03 23:13