A
A
A_Codzy2016-04-15 00:07:31
Parsing
A_Codzy, 2016-04-15 00:07:31

How does HTML parsing work in Swift?

I started learning Swift using the example of a simple application in which I want to write data from a specific site without an API to a TableView. Googling led me to the Kanna library for parsing HTML content.
Work example:

import Kanna

let html = "<html>...</html>"

if let doc = Kanna.HTML(html: html, encoding: NSUTF8StringEncoding) {
    print(doc.title)

    // Search for nodes by CSS
    for link in doc.css("a, link") {
        print(link.text)
        print(link["href"])
    }

    // Search for nodes by XPath
    for link in doc.xpath("//a | //link") {
        print(link.text)
        print(link["href"])
    }
}

I do not understand how it helps to pull out the necessary pieces of the page to my program. And how does this parsing process work through xpath or css

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
A_Codzy, 2016-04-17
@A_Codzy

I understand. Just had to read about xpath

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question