scrape a website in swift

Web scraping in Swift can be done by making use of the URLSession and HTML parsing libraries. Here's a simplified example:

main.swift
import Foundation
import SwiftSoup

let url = URL(string: "https://www.example.com")!
let task = URLSession.shared.dataTask(with: url) { data, response, error in
    guard let data = data else {
        print("\(error?.localizedDescription ?? "Unknown error")")
        return
    }
    
    do {
        let html = String(data: data, encoding: .utf8)
        let doc = try SwiftSoup.parse(html!)
        
        // extract specific elements with CSS selectors
        let title = try doc.select("title").text()
        let paragraphs = try doc.select("p").map { try $0.text() }
        
        // do something with the extracted data
        print("Title: \(title)")
        print("Paragraphs: \(paragraphs)")

    } catch {
        print("Error: \(error)")
    }
}
task.resume()
783 chars
28 lines

In the above code, we create a URL object for the website we want to scrape, and then create a URLSessionDataTask with the shared URLSession. We then extract the HTML from the response data, and use SwiftSoup to parse and extract data from specific elements using CSS selectors.

This is a simplified example, and you may need to customize it for more complex websites or data extraction needs. Note that some websites may have robots.txt files that prohibit scraping, so make sure to check for that before scraping a website.

gistlibby LogSnag