HXT組HTML錶行我想處理(定義很差)HTML,它在對的行分組的信息,像這樣:在哈斯克爾
<html>
<body>
<table>
<tr>
<td>
<font >
<a href="a">ABC</a></font>
</td>
</tr>
<tr>
<td height="50">
<font>When:</font><font>19-1-2013</font>
<b><font> </font></b>
<font>Where:</font><font>Here</font>
<font>Who:</font><font>Me</font>
</td>
</tr>
<tr>
<td>
<font >
<a href="b">EFG</a>
</font>
</td>
</tr>
<tr>
<td height="50">
<font>When:</font><font>19-2-2013</font>
<b><font> </font></b>
<font>Where:</font><font>There</font>
<font>Who:</font><font>You</font>
</td>
</tr>
<tr>
<td>
<font >
<a href="c">HIJ</a>
</font>
</td>
</tr>
<tr>
<td height="50">
<font>When:</font><font>19-3-2013</font><b>
<font> </font></b>
<font>Where:</font><font>Far away</font>
<font>Who:</font><font>Him</font>
</td>
</tr>
</table>
</body>
</html>
對此,幾經反覆,我來到這個代碼來實現我想要什麼:
import Data.List
import Control.Arrow.ArrowNavigatableTree
import Text.XML.HXT.Core
import Text.HandsomeSoup
group2 [] = []
group2 (x0:x1:xs) = [x0,x1]:(group2 xs)
countRows html = html >>> deep (hasName "tr") >. length
parsePage sz html = let
n x = deep (hasName "tr") >. ((-> a !! x) . group2) >>> unlistA
m = deep (hasName "td") >>> css "a" /> getText
o = deep (hasName "td") >>> hasAttr "height" >>> (css "font" >. (take 1 . drop 4)) >>> unlistA /> getText
p x = (((n x) >>> m) &&& ((n x) >>> o))
in html >>> catA [p x | x <- [0..sz]]
main = do
dt <- readFile "test.html"
let html = parseHtml dt
count <- (runX . countRows) html
let cnt = ((head count) `div` 2) - 1
prcssd <- (runX . (parsePage cnt)) html
print prcssd
,其結果是: [( 「ABC」, 「在這裏」),( 「EFG」, 「有」),( 「HIJ」 「遠」)]
但是,我不認爲這是一個非常好的答案,不得不先計算行數。使用HXT進行分組有更好的方法嗎?我已經嘗試運營商運氣不好。
extract multiples html tables with hxt這個問題雖然有用,但我認爲這個問題比較簡單。
就是這樣。我已經成對地將它們分組了,但是對我來說沒有發生這樣的事情,一個疙瘩是最合適的解決方案,因此允許我使用(***)。謝謝!然後,我也想從'屬性'行抓取多個字段,因此我只需修改你的函數:'insideFont =(deep(hasName「font」)>>。(\ x→[x !! 1 ])/> getText)&&&(deep(hasName「font」)>>。(\ x→[x !! 4])/> getText)'。這當然不是問題所在,當然,我只是把它包含在其他人可能會覺得有用的情況下。 – jcristovao 2013-02-20 15:00:29