1
我是編程新手,F#是我的第一語言。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#交互式測試功能fetchHtmlAsync和getFighterNameFromPage。他們都很好。
當我構建和運行解決方案,但是,我得到了以下錯誤消息:
An unhandled exception of type 'System.Net.WebException' occurred in FSharp.Core.dll Additional information: The remote server returned an error: (404) Not Found.
出了什麼問題?我應該做什麼改變?
笑... 6秒時:d – Carsten 2015-03-31 17:20:40
哈哈是啊,我只是評論同樣的事情在你的答案 – albertjan 2015-03-31 17:21:36
謝謝,albertjan!事實證明,我是一個白癡......我最初是按照你的方式編寫我的代碼,但因爲我忘記重建我的解決方案,所以我一直收到一條錯誤消息,這促使我開始做出不必要和不準確的更改。現在我剛剛重建,一切都很好。再次感謝你的幫助。 :-) – 2015-03-31 17:44:42