HandsomeSoup:获得工作的基本范例

时间:2013-11-10 16:45:43

标签: haskell html-parsing hxt

我试图从http://egonschiele.github.io/HandsomeSoup/获取基本示例:

main = do
    doc <- fromUrl "http://www.google.com/search?q=egon+schiele"
    links <- runX $ doc >>> css "h3.r a" ! "href"
    mapM_ putStrLn links

我试图重现这样的例子:

module Main (main) where
import Text.HandsomeSoup
import Text.XML.HXT.Core
import Control.Monad

main = do
    doc <- fromUrl "http://www.google.com/search?q=egon+schiele"
    links <- runX $ doc >>> css "h3.r a" ! "href"
    mapM_ putStrLn links        

但是,我收到以下错误:

$ runhaskell Main.hs

Main.hs:8:21:
    Couldn't match expected type `IOSLA (XIOState ()) XmlTree b0'
                with actual type `hxt-9.3.0.1:Data.Tree.NTree.TypeDefs.NTree
                                    hxt-9.3.0.1:Text.XML.HXT.DOM.TypeDefs.XNode'
    In the first argument of `(>>>)', namely `doc'
    In the second argument of `($)', namely
      `doc >>> css "h3.r a" ! "href"'
    In a stmt of a 'do' block:
      links <- runX $ doc >>> css "h3.r a" ! "href"

但我似乎无法弄清楚发生了什么。

1 个答案:

答案 0 :(得分:3)

fromUrl的函数类型为fromUll :: String - &gt; IOSArrow XmlTree(NTree XNode)。 所以IOSArrow XmlTree(NTree XNode)并不清楚IO动作。

最简单的方法修复它 - 改为使用let语句:

import Text.HandsomeSoup
import Text.XML.HXT.Core

main :: IO ()
main = do
  let doc = fromUrl "http://www.google.com/search?q=egon+schiele"
  links <- runX $ doc >>> css "h3.r a" ! "href"
  mapM_ putStrLn links