Parsing HTML in Swift (iOS/macOS): SwiftSoup and Fuzi Libraries

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Parsing HTML in Swift (iOS/macOS): Overview of SwiftSoup and Fuzi Libraries



When developing applications for iOS and macOS, there is often a need to extract data from web pages. HTML parsing allows you to automatically retrieve content, structure it, and use it in your application. In the Swift ecosystem, there are several powerful libraries for this purpose. In this article, we will take a detailed look at the two most popular ones: SwiftSoup and Fuzi. You will learn how to install them, how to perform basic parsing operations, and what features each one has.



Why is HTML parsing needed in Swift?

HTML parsing is useful in the following scenarios:

  • Data aggregation — collecting news, prices, product descriptions from multiple websites.
  • Change monitoring — tracking updates on target pages.
  • Integration with web services — if a website does not provide an API, parsing is the only way to get data.
  • Test automation — checking the correctness of the HTML structure.


SwiftSoup Library

SwiftSoup is a Swift port of the popular Java library Jsoup. It provides a convenient API for parsing, navigating, and manipulating HTML documents. SwiftSoup supports CSS selectors, making element extraction simple and intuitive.



Installing SwiftSoup

Add the dependency to your Package.swift or via Swift Package Manager in Xcode:

// Package.swiftdependencies: [    .package(url: "https://github.com/scinfu/SwiftSoup.git", from: "2.6.0")]


Example of using SwiftSoup

Let's look at a simple example: we'll load an HTML string and extract <h2> headings and <a> links.

import SwiftSoup

func parseHTML() { let html = """ <html> <body> <h2>Article Title</h2> <p>Paragraph text.</p> <a href="https://example.com">Link</a> </body> </html> """ do { let doc = try SwiftSoup.parse(html) // Extract all h2 headings let headers = try doc.select("h2") for header in headers { print("Heading: \\(try header.text())") } // Extract all links let links = try doc.select("a[href]") for link in links { let linkText = try link.text() let linkHref = try link.attr("href") print("Text: \\(linkText), URL: \\(linkHref)") } } catch { print("Parsing error: \\(error)") }}

In this example, we use the select() method with the CSS selector "h2" to select all second-level headings, and "a[href]" to select all links with the href attribute. The text() method returns the text content of the element, and attr() returns the value of the specified attribute.



Navigating the Document Tree

SwiftSoup allows you to traverse the DOM tree:

// Get the first h2 elementif let firstHeader = try doc.select("h2").first() {    print("First heading: \\(try firstHeader.text())")}

// Get the parent elementlet parent = try firstHeader.parent()print("Parent: \\(try parent?.tagName() ?? "nil")")

// Get the next sibling elementlet nextSibling = try firstHeader.nextElementSibling()print("Next element: \\(try nextSibling?.tagName() ?? "nil")")


Fuzi Library

Fuzi is another popular library for parsing HTML and XML in Swift. It is based on libxml2 and is known for its high performance. Fuzi also supports CSS selectors and XPath queries.

Recommendations