我正在編寫簡單的sitemap.xml爬行程序。代碼如下。我的問題是爲什麼main
末尾的代碼不能打印任何內容。我懷疑這是因爲Haskell的lazyness但不知道這裏怎麼對付它:Haskell不評估塊
import Network.HTTP.Conduit
import qualified Data.ByteString.Lazy as L
import Text.XML.Light
import Control.Monad.Trans (liftIO)
import Control.Monad
import Data.String.Utils
import Control.Exception
download :: Manager -> Request -> IO (Either HttpException L.ByteString)
download manager req = do
try $
fmap responseBody (httpLbs req manager)
downloadUrl :: Manager -> String -> IO (Either HttpException L.ByteString)
downloadUrl manager url = do
request <- parseUrl url
download manager request
getPages :: Manager -> [String] -> IO [Either HttpException L.ByteString]
getPages manager urls =
sequence $ map (downloadUrl manager) urls
main = withManager $ \ manager -> do
-- I know simpleHttp is bad here
mapSource <- liftIO $ simpleHttp "http://example.com/sitemap.xml"
let elements = (parseXMLDoc mapSource) >>= Just . findElements (mapElement "loc")
Just urls = liftM (map $ (replace "/#!" "?_escaped_fragment_=") . strContent) elements
mapElement name = QName name (Just "http://www.sitemaps.org/schemas/sitemap/0.9") Nothing
return $
getPages manager urls >>= \ pages -> do
print "evaluate me!"
sequence $ map print pages
你爲什麼要把'getPages'包裝在'return'中?這似乎沒有必要。 – arrowd
@arrowdodger沒有返回我得到編譯錯誤:'無法匹配類型'IO' 'Control.Monad.Trans.Resource.Internal.ResourceT m' 預期類型:Control.Monad.Trans.Resource.Internal.ResourceT m [要麼是HttpException L.ByteString] 實際類型:IO [要麼HttpException L.ByteString]' –