1

希望使用 HXT 从格式良好的 HTMl 表中的表中提取记录。我已经查看了一些关于 SO 和 HXT 文档的示例,例如:

我的问题是:

我想通过已知的 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 个。)

关于如何更轻松地做到这一点的任何进一步的一般提示。

4

1 回答 1

1

HXT/Practical/Google1我想我能够拼凑出一个解决方案。

{-# LANGUAGE Arrows #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Hanzo where
import Text.HandsomeSoup
import Text.XML.HXT.Cor

atTag tag =
  deep (isElem >>> hasName tag)
text =
  deep isText >>> getText

data Rock = Rock String String String deriving Show    
rocks =
  atTag "tbody" //> atTag "tr"
  >>> proc x -> do
        rowID <- x >- getAttrValue "id"
        name <- x >- atTag "td" >. (!! 0) >>> text
        kind <- x >- atTag "td" >. (!! 1) >>> text
        returnA -< Rock rowID name kind

main = do
  dt <- readFile "html.html"
  result <- runX $ parseHtml dt
                   //> hasAttrValue "id" (== "Greatest-Table")
                   >>> rocks
  print result

关键点如下:

  • 您的箭头适用于元素,但不适用于单个元素。这是ArrowList约束。因此,调用getText3 次会产生令人惊讶的行为,因为getText它代表了在<table>通过proc x -> do {...}.

  • 相反,我们可以做的是专注于我们想要的流:<tr><tbody>. 对于每个表行,我们获取 ID 属性值和前两个<td>s 的文本。

  • 这似乎不是最优雅的解决方案,但我们可以索引到流的一种方法是使用(>.) :: ArrowList cat => cat a b -> ([b] -> c) -> cat a c组合器对其进行过滤。

  • 最后一个技巧,我在实际 wiki 示例中注意到的一个技巧:我们可以使用deepisElem/isText专注于我们想要的节点。XML 树很吵!

于 2016-12-19T08:18:05.580 回答