Download the HTML source from a website

Daily Coding Tip 001

Here’s how to get the underlying HTML from a webpage.

import Foundation
func getHTMLSource() -> String {
//Create the URL
guard let url = URL(string: "https://apple.com") else {
fatalError("Could not create URL")
}
// Try downloading the HTML source
do {
return try String(contentsOf: url, encoding: .utf8)
}
//If there is an error
catch let error {
fatalError("Failed to download HTML due to error: \(error.localizedDescription)")
}
}
view raw getHTMLSource.swift hosted with ❤ by GitHub

Creating the source URL

I’m using the Apple website in my example, but you can use whatever you want. If you’re unfamiliar with guard let, this is allowing us to optionally bind the result of an optional initialiser. URL can fail and return nil, depending on whether the String provided has a valid structure. If we successfully create a URL, it will be accessible through the constant value url.

Since being unable to use the URL would completely break our code, I’m using a fatalError when the initialisation fails and returns nil.

Get the HTML source

Assuming that the URL was created successfully, we need to download the HTML source. This uses an initialiser for String that can throw an error, so we do it inside a do block. Our function returns the resulting HTML String if no error is thrown, otherwise the function fails with an error that’s caught in the catch block.

Isn’t fatalError a bit extreme?

I could have used assertionFailure for the two possible failure scenarios, but if you do that you’ll notice something. Since I expect this function to succeed, it returns a String that isn’t optional. Therefore all paths through the function should either return the expected value or crash the entire app.

If I was to use assertionFailure I would need to either return an empty String or make the return type optional and return nil.


Get more Daily Coding Tips in your inbox!