懒惰的字节串疯狂

时间:2015-12-17 12:08:21

标签: haskell

我真的在努力摆弄这个圈子。

getPostContent使用Wreq下载博客文章并将其返回。

getPostContent url = do
    let opts = defaults & W.checkStatus .~ (Just $ \_ _ _ -> Nothing)

    postResp <- getWith opts $ baseUrl ++ url
    if postResp ^. W.responseStatus . statusCode == 200
        -- then return $ LEnc.encodeUtf8 $ postResp ^. W.responseBody . _String      -- :: Prism T Text
        then return $ postResp ^. W.responseBody . _String
        else return "error downloading"

这是由parseLBS消耗的

do
     page <- getPostContent r -- :: IO String
     let
     -- parseLBS :: Data.ByteString.Lazy.Internal.ByteString -> Text.XML.Document
     cursor = fromDocument $ parseLBS page

根据我的理解,getPostContent正在提供Data.Text.Text,而我需要Data.ByteString.Lazy.Internal.ByteString而我无法解决如何转换它们(认为它应该是this,请参阅上面的代码段,但它也没有编译。)

   Couldn't match expected type ‘Data.ByteString.Lazy.Internal.ByteString’
                with actual type ‘T.Text’
    In the first argument of ‘parseLBS’, namely ‘page’
    In the second argument of ‘($)’, namely ‘parseLBS page’

带编码未注释的编译消息

Couldn't match type ‘TL.Text’
               with ‘T.Text’
NB: ‘TL.Text’ is defined in ‘Data.Text.Internal.Lazy’
    ‘T.Text’ is defined in ‘Data.Text.Internal’
Expected type: (TL.Text -> Const TL.Text TL.Text)
               -> Data.ByteString.Lazy.Internal.ByteString
               -> Const TL.Text Data.ByteString.Lazy.Internal.ByteString
  Actual type: (T.Text -> Const TL.Text T.Text)
               -> Data.ByteString.Lazy.Internal.ByteString
               -> Const TL.Text Data.ByteString.Lazy.Internal.ByteString
In the second argument of ‘(.)’, namely ‘_String’
In the second argument of ‘(^.)’, namely ‘responseBody . _String’

1 个答案:

答案 0 :(得分:2)

总结一下:encodeUtf8是正确的方法。您使用的似乎来自Data.Text.Lazy.Encoding,这需要一个懒惰的Text。您可以使用Data.Text.Lazy.fromStrict转换...或者您可以查看Data.Text.Encoding,它适用于严格的Text(但随后会给您一个严格的ByteString ...)< / p>