A lightweight extension that combines Alamofire and SwiftSoup for easy HTML parsing from web requests.
Add the following to your Package.swift dependencies:
.package(url: "https://github.com/your-username/AlamofireSwiftSoup.git", from: "1.0.0")And add "AlamofireSwiftSoup" to your target dependencies.
- iOS 15+ / macOS 12+ / tvOS 13+
- Swift 5.9+
- Alamofire 5.x
- SwiftSoup 2.x
import Alamofire
import AlamofireSwiftSoup
AF.request("https://example.com").responseHTML { response in
switch response.result {
case .success(let document):
if let title = try? document.title() {
print("Page title:", title)
}
case .failure(let error):
print("Failed to parse HTML:", error)
}
}This package adds a custom Alamofire response serializer that:
- Downloads HTML content using Alamofire
- Parses the raw HTML into a
SwiftSoup.Document - Returns the document in the
responseHTMLcallback
AF.request("https://news.ycombinator.com").responseHTML { response in
if let document = try? response.result.get() {
let links = try? document.select("a.storylink")
links?.forEach { print(try? $0.text()) }
}
}MIT License. See LICENSE for details.