1

我是编程新手,F# 是我的第一语言。

以下是我的代码的相关片段:

let downloadHtmlToDiskAsync (fighterHtmlDirectory: string) (fighterBaseUrl: string) (fighterId: int) = 
    let fighterUrl = fighterBaseUrl + fighterId.ToString()
    try 
        async {

            let! html = fetchHtmlAsync fighterUrl
            let fighterName = getFighterNameFromPage html

            let newTextFile = File.Create(fighterHtmlDirectory + "\\" + fighterId.ToString("00000") + " " + fighterName.TrimEnd([|' '|]) + ".html")
            use file = new StreamWriter(newTextFile) 
            file.Write(html) 
            file.Close()
        }
    with
        :? System.Net.WebException -> async {File.AppendAllText("G:\User\WebScraping\Invalid Urls.txt", fighterUrl + "\n")}

let downloadFighterDatabase (directoryPath: string) (fighterBaseUrl: string) (beginningFighterId: int) (endFighterId: int) =
    let allFighterIds = [for id in beginningFighterId .. endFighterId -> id]
    allFighterIds
    |> Seq.map (fun fighterId -> downloadHtmlToDiskAsync directoryPath fighterBaseUrl fighterId)
    |> Async.Parallel
    |> Async.RunSynchronously

我已经使用 F# Interactive 测试了 fetchHtmlAsync 和 getFighterNameFromPage 函数。他们都工作正常。

但是,当我构建并运行解决方案时,我收到以下错误消息:

FSharp.Core.dll 中出现“System.Net.WebException”类型的未处理异常附加信息:远程服务器返回错误:(404) 未找到。

什么地方出了错?我应该做出哪些改变?

4

1 回答 1

3

把你的try with里面async

let downloadHtmlToDiskAsync (fighterHtmlDirectory: string) (fighterBaseUrl: string) (fighterId: int) = 
    let fighterUrl = fighterBaseUrl + fighterId.ToString()
    async {
        try
            let! html = fetchHtmlAsync fighterUrl
            let fighterName = getFighterNameFromPage html

            let newTextFile = File.Create(fighterHtmlDirectory + "\\" + fighterId.ToString("00000") + " " + fighterName.TrimEnd([|' '|]) + ".html")
            use file = new StreamWriter(newTextFile) 
            file.Write(html) 
            file.Close()
        with
            :? System.Net.WebException -> File.AppendAllText("G:\User\WebScraping\Invalid Urls.txt", fighterUrl + "\n")
    }
于 2015-03-31T17:20:17.700 回答