I am new to programming and F# is my first language. I am currently still very unfamiliar with .NET APIs.
As a beginner's project, I want to scrape a website. I want to write a function that, given a specific URL, automatically downloads all the HTML contents on that page. However, if the URL is invalid, rather than throwing a System.Net.WebException message, I want to return the Boolean output "False" instead.
Here is the relevant part of my code:
let noSuchURL (url: string) =
let html = downloadHtmlFromUrl url
let regexPattern = @"<title>Page not found</title>"
let matchResult = Regex.IsMatch(html, regexPattern)
matchResult
(I have tested the downloadHtmlFromUrl function in F# interactive, and it works fine.)
I realised that the code above does not return a Boolean value in the event that the address is invalid. Instead, System.Net.WebException is thrown, with the message "System.Net.WebException: The remote server returned an error: (404) Not Found".
What changes can I make to get a Boolean output?
Aucun commentaire:
Enregistrer un commentaire