希望使用 HXT 从格式良好的 HTMl 表中的表中提取记录。我已经查看了一些关于 SO 和 HXT 文档的示例,例如:
- 从子树中提取值
- http://adit.io/posts/2012-04-14-working_with_HTML_in_haskell.html
- https://www.schoolofhaskell.com/school/advanced-haskell/xml-parsing-with-validation
- 在 IO 之外运行 Haskell HXT?
- 用hxt提取多个html表
- 在haskell中解析html
- http://neilbartlett.name/blog/2007/08/01/haskell-explaining-arrows-through-xml-transformationa/
- https://wiki.haskell.org/HXT/Practical/Simple2
- https://wiki.haskell.org/HXT/Practical/Simple1
- 在 Haskell 中使用 HXT 对 html 表行进行分组
- 使用 HXT 解析 Haskell 中的多个子节点
我的问题是:
我想通过已知的 id 唯一地标识一个表,然后为该表中的每个 tr 创建一个记录对象并将其作为记录列表返回。
这是我的 HTML
<!DOCTYPE html>
<head>
<title>FakeHTML</title>
</head>
<body>
<table id="fakeout-dont-get-me">
<thead><tr><td>Null</td></tr></thead>
<tbody><tr><td>Junk!</td></tr></tbody>
</table>
<table id="Greatest-Table">
<thead>
<tr><td>Name</td><td>Favorite Rock</td></tr>
</thead>
<tbody>
<tr id="rock1">
<td>Fred</td>
<td>Igneous</td>
</tr>
<tr id="rock2">
<td>Bill</td>
<td>Sedimentary</td>
</tr>
</tbody>
</table>
</body>
</html>
这是我正在尝试的代码,以及两种不同的解析方法。首先,进口...
{-# LANGUAGE Arrows, OverloadedStrings, DeriveDataTypeable, FlexibleContexts #-}
import Text.XML.HXT.Core
import Text.HandsomeSoup
import Text.XML.HXT.XPath.XPathEval
import Data.Tree.NTree.TypeDefs
import Text.XML.HXT.XPath.Arrows
我想要的是 Rockrecs 的列表,例如来自...
recs = [("rock1", "Name", "Fred", "Favorite Rock", "Igneous"),
("rock2", "Name", "Bill", "Favorite Rock", "Sedimentary")]
data Rockrec = Rockrec { rockID:: String,
rockName :: String,
rockFav :: String} deriving Show
rocks = [(\(a,_,b,_,c) -> Rockrec a b c ) r | r <- recs]
-- [Rockrec {rockID = "rock1", rockName = "Fred", rockFav = "Igneous"},
-- Rockrec {rockID = "rock2", rockName = "Bill", rockFav = "Sedimentary"}]
这是我的第一种方法,它在我返回一堆 [XMLTree] 后在 runLA 上使用绑定。也就是说,我进行第一次解析只是为了得到正确的表,然后在第一次抓取之后处理树行。
尝试 1
getTab = do
dt <- Prelude.readFile "fake.html"
let html = parseHtml dt
tab <- runX $ html //> hasAttrValue "id" (== "Greatest-Table")
return tab
-- hmm, now this gets tricky...
-- table <- getTab
node tag = multi (hasName tag)
-- a la https://stackoverflow.com/questions/3901492/running-haskell-hxt-outside-of-io?rq=1
getIt :: ArrowXml cat => cat (Data.Tree.NTree.TypeDefs.NTree XNode) (String, String)
getIt = (node "tr" >>>
(getAttrValue "id" &&& (node "td" //> getText)))
这有点工作。我需要按摩一下,但可以让它运行......
-- table >>= runLA getIt
-- [("","Name"),("","Favorite Rock"),("rock1","Fred"),("rock1","Igneous"),("rock2","Bill"),("rock2","Sedimentary")]
这是第二种方法,灵感来自 https://wiki.haskell.org/HXT/Practical/Simple1。在这里,我认为我依赖于 {-# LANGUAGE Arrows -} 中的某些内容(巧合的是,这打破了我对上面 rec 的列表理解),以使用 proc 函数在更具可读性的 do 块中执行此操作。也就是说,我什至无法编译它的最小版本:
尝试 2
getR :: ArrowXml cat => cat XmlTree Rockrec
getR = (hasAttrValue "id" (== "Greatest-Table")) >>>
proc x -> do
rockId <- getText -< x
rockName <- getText -< x
rockFav <- getText -< x
returnA -< Rockrec rockId rockName rockFav
编辑
类型问题,回应亚历克的以下评论
λ> getR [table]
<interactive>:56:1-12: error:
• Couldn't match type ‘NTree XNode’ with ‘[[XmlTree]]’
Expected type: [[XmlTree]] -> Rockrec
Actual type: XmlTree -> Rockrec
• The function ‘getR’ is applied to one argument,
its type is ‘cat0 XmlTree Rockrec’,
it is specialized to ‘XmlTree -> Rockrec’
In the expression: getR [table]
In an equation for ‘it’: it = getR [table]
λ> getR table
<interactive>:57:1-10: error:
• Couldn't match type ‘NTree XNode’ with ‘[XmlTree]’
Expected type: [XmlTree] -> Rockrec
Actual type: XmlTree -> Rockrec
• The function ‘getR’ is applied to one argument,
its type is ‘cat0 XmlTree Rockrec’,
it is specialized to ‘XmlTree -> Rockrec’
In the expression: getR table
In an equation for ‘it’: it = getR table
结束编辑
即使我没有选择元素,我也无法运行上述内容。我也有点困惑,我应该如何做一些事情,比如将第一个 td 放在 rockName 中,将第二个 td 放在 rockFav 中,如何在这些上包含一个迭代器(假设我有很多 td 字段,而不是只有 2 个。)
关于如何更轻松地做到这一点的任何进一步的一般提示。