1

我正在尝试从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"

但我真的无法弄清楚发生了什么。

4

1 回答 1

3

函数 fromUrl 的类型为 fromUrl :: String -> IOSArrow XmlTree (NTree XNode)。所以IOSArrow XmlTree(NTree XNode)不清楚IO-action。

修复它的最简单方法 - 改用 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
于 2013-11-10T17:34:11.407 回答