Asynchronous F#:处理web异常
我是编程新手,F#是我的第一语言 以下是我的代码的相关片段:Asynchronous F#:处理web异常,asynchronous,f#,system.net.webexception,Asynchronous,F#,System.net.webexception,我是编程新手,F#是我的第一语言 以下是我的代码的相关片段: let downloadHtmlToDiskAsync (fighterHtmlDirectory: string) (fighterBaseUrl: string) (fighterId: int) = let fighterUrl = fighterBaseUrl + fighterId.ToString() try async { let! html = fetchH
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)未找到
出了什么问题?我应该做哪些更改?将您的
放入异步中,然后尝试
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")
}
哈哈,是的,我只是在你的回答上评论了完全相同的事情谢谢,阿尔伯特扬!原来我是个白痴。。。我最初是按照您的方式编写代码的,但由于我忘记重新生成解决方案,我不断收到一条错误消息,这促使我开始进行不必要和不准确的更改。现在我刚刚重建,一切都很顺利。再次感谢您的帮助。:-)这发生在我们当中最好的人身上:)