-5

I am just getting started with Swift. I have done a ton of webscrping with B4S in Python, but I am having trouble with Swift.

Does anyone know how I can request html data from a website?

How would I get the html from http://www.example.com/

Or does anyone have a site they would recommend to get started?

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116

1 Answers1

0

Does anyone know how I can request html data from a website?

As rmaddy notes, URLSession.

import Foundation

let url = URL(string: "http://www.example.com")!
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
    if  let data = data,
        let string = String(data: data, encoding: .utf8)
    {
        print(string)
    }
}
task.resume()
Rob Napier
  • 286,113
  • 34
  • 456
  • 610