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