首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >F#:突破循环

F#:突破循环
EN

Stack Overflow用户
提问于 2015-04-01 12:54:46
回答 1查看 222关注 0票数 2

我对编程很陌生,F#是我的第一语言。

我有一个URL列表,当第一次访问时,这些URL要么返回HTTP错误404,要么经历网关超时。对于这些URL,我想尝试再访问3次。在这3次尝试结束时,如果仍然引发WebException错误,我将假设该URL不存在,并将其添加到包含所有无效URL的文本文件中。

这是我的代码:

代码语言:javascript
复制
let tryAccessingAgain (url: string) (numAttempts: int) =
    async {
        for attempt = 1 to numAttempts do
            try 
                let! html = fetchHtmlAsync url
                let name = getNameFromPage html

                let id = getIdFromUrl url

                let newTextFile = File.Create(htmlDirectory + "\\" + id.ToString("00000") + " " + name.TrimEnd([|' '|]) + ".html")
                use file = new StreamWriter(newTextFile) 
                file.Write(html) 
                file.Close()
            with
                :? System.Net.WebException -> File.AppendAllText("G:\User\Invalid URLs.txt", url + "\n")
        }

我已经在fetchHtmlAsync,getNameFromPage和getIdFromUrl的F#互动测试过。他们都很好用。

如果我成功地下载了一个URL的HTML内容,而没有使用所有三次尝试,显然我想立即突破for-循环。我的问题是:我怎样才能这样做?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-01 13:05:01

使用递归代替循环:

代码语言:javascript
复制
let rec tryAccessingAgain (url: string) (numAttempts: int) =
    async {
        if numAttempts > 0 then
            try 
                let! html = fetchHtmlAsync url
                let name = getNameFromPage html

                let id = getIdFromUrl url

                let newTextFile = File.Create(htmlDirectory + "\\" + id.ToString("00000") + " " + name.TrimEnd([|' '|]) + ".html")
                use file = new StreamWriter(newTextFile) 
                file.Write(html) 
                file.Close()
            with
            | :? System.Net.WebException -> 
                File.AppendAllText("G:\User\Invalid URLs.txt", url + "\n")
                return! tryAccessingAgain url (numAttempts-1)
        }

请注意,我无法测试它,并且可能有一些语法错误-对不起,如果

正如我们所做的--您可能需要像这样重写无效url的日志:

代码语言:javascript
复制
let rec tryAccessingAgain (url: string) (numAttempts: int) =
    async {
        if numAttempts <= 0 then
            File.AppendAllText("G:\User\Invalid URLs.txt", url + "\n")
        else
            try 
                let! html = fetchHtmlAsync url
                let name = getNameFromPage html

                let id = getIdFromUrl url

                let newTextFile = File.Create(htmlDirectory + "\\" + id.ToString("00000") + " " + name.TrimEnd([|' '|]) + ".html")
                use file = new StreamWriter(newTextFile) 
                file.Write(html) 
                file.Close()
            with
            | :? System.Net.WebException -> 
                return! tryAccessingAgain url (numAttempts-1)
        }

这样,它只会被记录一次所有的尝试。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29391393

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档