gpt4 book ai didi

haskell - 如何使用 Haskell 从 Internet 下载文件?

转载 作者:行者123 更新时间:2023-12-04 11:58:34 24 4
gpt4 key购买 nike

我只是想做一些类似于 wget 的事情,我从 Internet 下载文件。我看到以前有个包叫http-wget ,但它已被弃用以支持 http-conduit。

Http-conduit 有 a simple example了解如何使用 httpBS 获取网页内容。因此,在此之后,我得到了这个工作:

{-# LANGUAGE OverloadedStrings #-}

import Network.HTTP.Simple
import qualified Data.ByteString.Char8 as B8

main :: IO ()
main = do
let url = "https://www.example.com/sitemap.xml"
resp <- httpBS url
B8.putStrLn $ getResponseBody resp

这适用于从 URL 获取文件名(sitemap.xml):

{-# LANGUAGE OverloadedStrings #-}

import Network.HTTP.Simple
import qualified Data.ByteString.Char8 as B8

main :: IO ()
main = do
let url = "https://www.example.com/sitemap.xml"
let urlParts = B8.split '/' $ B8.pack url
let fileName = Prelude.last urlParts
B8.putStrLn fileName

但我不能把它们放在一起:

{-# LANGUAGE OverloadedStrings #-}

import Network.HTTP.Simple
import qualified Data.ByteString.Char8 as B8

main :: IO ()
main = do
let url = "https://www.example.com/sitemap.xml"
let urlParts = B8.split '/' $ B8.pack url
let fileName = Prelude.last urlParts
resp <- httpBS url
B8.putStrLn $ getResponseBody resp

这给出了错误:

ny1920-parse.hs:12:41: error:
• Couldn't match type ‘Request’ with ‘[Char]’
Expected type: String
Actual type: Request
• In the first argument of ‘B8.pack’, namely ‘url’
In the second argument of ‘($)’, namely ‘B8.pack url’
In the expression: B8.split '/' $ B8.pack url
|
12 | let urlParts = B8.split '/' $ B8.pack url
| ^^^

所以我只需要转换String -> Request?显然有 a function for that in http-conduit ,但它没有按预期工作 - 我仍然遇到同样的错误。

我可以强制 URL 是这样的请求:

  let url = "https://www.example.com/sitemap.xml" :: Request

但是当然这会破坏我分解文件名的部分,因为它需要一个 [Char] 而不是 Request

所以我被困住了——如果我将 URL 设为字符串,它会破坏 http-conduit。如果我把它作为一个请求,它会破坏字符串操作。

我觉得这么简单的事情不应该这么难,不是吗?

编辑:好的,所以我几乎可以让它与这个添加一起工作:

  let urlParts = B8.split '/' $ B8.pack (show url)

编译,但它使文件名损坏。尝试打印出文件名给出: "1.1\n}\n" 而不是 sitemap.xml

最佳答案

我不同意这里的另一个答案:自己拆分 / 是个坏主意。不要尝试实现临时 URL 解析器;这比你想象的要难。相反,请重新使用您已有的解析:

{-# LANGUAGE OverloadedStrings #-}

import Network.HTTP.Client
import Network.HTTP.Simple
import Network.URI
import qualified Data.ByteString.Char8 as B8

main :: IO ()
main = do
let request = "https://www.example.com/sitemap.xml"
fileName = Prelude.last . pathSegments . getUri $ request
resp <- httpBS request
B8.putStrLn $ getResponseBody resp

the documentation有关可以从 URI 中提取的部分的更多信息。

关于haskell - 如何使用 Haskell 从 Internet 下载文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67945816/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com