Can SwiftSoup parse HTML from a string in Swift?

Yes, SwiftSoup is a Swift library that can parse HTML from a string. SwiftSoup provides a set of powerful APIs inspired by the popular Java library Jsoup. It allows Swift developers to parse, traverse, and manipulate HTML documents in a type-safe and convenient manner.

To use SwiftSoup to parse HTML from a string in Swift, you first need to add the SwiftSoup dependency to your project. If you are using CocoaPods, you can add it to your Podfile:

pod 'SwiftSoup'

Or if you are using Swift Package Manager, you can add it to your Package.swift dependencies:

dependencies: [
    .package(url: "https://github.com/scinfu/SwiftSoup.git", from: "2.3.2")
]

Once you have SwiftSoup integrated into your project, you can use it to parse an HTML string as follows:

import SwiftSoup

let htmlString = """
    <html>
    <head>
        <title>Sample Title</title>
    </head>
    <body>
        <p class="description">This is a sample paragraph.</p>
    </body>
    </html>
"""

do {
    let doc: Document = try SwiftSoup.parse(htmlString)
    let title: String = try doc.title()
    let paragraph: Element? = try doc.select("p.description").first()

    print(title) // Output: Sample Title
    if let paragraphText = paragraph?.text() {
        print(paragraphText) // Output: This is a sample paragraph.
    }
} catch {
    print("Error parsing HTML: \(error)")
}

In this example, we import SwiftSoup and define an HTML string. We then use SwiftSoup.parse to parse the HTML string into a Document object. We extract the title using doc.title() and find the first paragraph with a class of description using doc.select("p.description").first(). We handle potential errors using Swift's do-catch error handling.

Remember to always handle errors appropriately when working with parsing libraries, as there are many potential points of failure, such as malformed HTML or incorrect selectors.

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon