我对编程很陌生,F#是我的第一语言。
我有一个URL列表,当第一次访问时,这些URL要么返回HTTP错误404,要么经历网关超时。对于这些URL,我想尝试再访问3次。在这3次尝试结束时,如果仍然引发WebException错误,我将假设该URL不存在,并将其添加到包含所有无效URL的文本文件中。
这是我的代码:
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-循环。我的问题是:我怎样才能这样做?
发布于 2015-04-01 13:05:01
使用递归代替循环:
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的日志:
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)
}这样,它只会被记录一次所有的尝试。
https://stackoverflow.com/questions/29391393
复制相似问题