Table of contents

How do I add SwiftSoup to my Swift project?

SwiftSoup is a Swift port of the popular Java library jsoup for parsing HTML. There are three main ways to add SwiftSoup to your Swift project: Swift Package Manager (recommended), CocoaPods, or Carthage.

Swift Package Manager (Recommended)

Swift Package Manager is the modern, Apple-recommended way to manage dependencies in Swift projects.

Using Xcode Interface

  1. Open your project in Xcode
  2. Go to FileAdd Package Dependencies...
  3. Enter the SwiftSoup repository URL: https://github.com/scinfu/SwiftSoup.git
  4. Select the version rule (usually "Up to Next Major Version")
  5. Click Add Package
  6. Select your target and click Add Package

Using Package.swift

For Swift packages, add SwiftSoup to your Package.swift file:

// swift-tools-version:5.5
import PackageDescription

let package = Package(
    name: "YourProject",
    platforms: [
        .iOS(.v12),
        .macOS(.v10_15),
        .tvOS(.v12),
        .watchOS(.v5)
    ],
    dependencies: [
        .package(url: "https://github.com/scinfu/SwiftSoup.git", from: "2.6.0")
    ],
    targets: [
        .target(
            name: "YourProject",
            dependencies: ["SwiftSoup"]
        ),
        .testTarget(
            name: "YourProjectTests",
            dependencies: ["YourProject", "SwiftSoup"]
        )
    ]
)

Then run:

swift package update

CocoaPods

Installation

  1. Install CocoaPods if you haven't already:
sudo gem install cocoapods
  1. Navigate to your project directory and initialize CocoaPods:
cd YourProjectDirectory
pod init
  1. Open the generated Podfile and add SwiftSoup:
platform :ios, '12.0'
use_frameworks!

target 'YourProjectName' do
    pod 'SwiftSoup', '~> 2.6'
end
  1. Install the dependency:
pod install
  1. Important: Always open the .xcworkspace file (not .xcodeproj) in Xcode after running pod install.

Carthage

Installation

  1. Install Carthage using Homebrew:
brew install carthage
  1. Create a Cartfile in your project root: github "scinfu/SwiftSoup" ~> 2.6

  2. Build the framework:

carthage update --platform iOS --use-xcframeworks
  1. In Xcode, go to your target's General tab
  2. Drag SwiftSoup.xcframework from Carthage/Build/ to Frameworks, Libraries, and Embedded Content

Verification and Basic Usage

After installation, import SwiftSoup in your Swift files:

import SwiftSoup

Test the installation with a simple example:

import SwiftSoup

func testSwiftSoup() {
    let html = "<html><head><title>Test</title></head><body><p>Hello World</p></body></html>"

    do {
        let doc: Document = try SwiftSoup.parse(html)
        let title = try doc.title()
        let paragraph = try doc.select("p").first()?.text()

        print("Title: \(title)")           // Output: Title: Test
        print("Paragraph: \(paragraph)")  // Output: Paragraph: Hello World
    } catch {
        print("Error: \(error)")
    }
}

Platform Support

SwiftSoup supports: - iOS 12.0+ - macOS 10.15+ - tvOS 12.0+ - watchOS 5.0+ - Linux (Swift 5.5+)

Choosing the Right Method

  • Swift Package Manager: Best for new projects and iOS 13+/macOS 10.15+ targets
  • CocoaPods: Good for projects already using CocoaPods or needing iOS 11 support
  • Carthage: Suitable for projects preferring decentralized dependency management

After successful installation, you can start parsing HTML, extracting data, and manipulating DOM elements using SwiftSoup's comprehensive API.

Try WebScraping.AI for Your Web Scraping Needs

Looking for a powerful web scraping solution? WebScraping.AI provides an LLM-powered API that combines Chromium JavaScript rendering with rotating proxies for reliable data extraction.

Key Features:

  • AI-powered extraction: Ask questions about web pages or extract structured data fields
  • JavaScript rendering: Full Chromium browser support for dynamic content
  • Rotating proxies: Datacenter and residential proxies from multiple countries
  • Easy integration: Simple REST API with SDKs for Python, Ruby, PHP, and more
  • Reliable & scalable: Built for developers who need consistent results

Getting Started:

Get page content with AI analysis:

curl "https://api.webscraping.ai/ai/question?url=https://example.com&question=What is the main topic?&api_key=YOUR_API_KEY"

Extract structured data:

curl "https://api.webscraping.ai/ai/fields?url=https://example.com&fields[title]=Page title&fields[price]=Product price&api_key=YOUR_API_KEY"

Try in request builder

Related Questions

Get Started Now

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