如何使用Swift从表中的HTML类检索标签

时间:2018-08-29 18:08:31

标签: swift shtml swiftsoup

尝试从以下HTML检索:“ 17,02”:

<div class="overflow-auto">
    <table class="w-100 tl mb4 mt3 f6" cellspacing="0">
        <thead>
            <tr>
                <th class="fw6 bb b--black-20 tl pb3 pr3 bg-white tl">Kvalitet</th>
                <th class="fw6 bb b--black-20 tl pb3 pr3 bg-white tl">Pris inkl. mva.</th>
                <th class="fw6 bb b--black-20 tl pb3 pr3 bg-white tl">Endring</th>
                <th class="fw6 bb b--black-20 tl pb3 pr3 bg-white tl">Gjeldene fra</th>
            </tr>
        </thead>
        <tbody class="lh-copy">
            <tr>
                <td class="pv3 pr3 bb b--black-20"><img src="./assets/95 Miles.png" alt="95 Miles"></td>
                <td class="pv3 pr3 bb b--black-20">Kr 17,02</td>
                <td class="pv3 pr3 bb b--black-20">5 øre</td>
                <td class="pv3 pr3 bb b--black-20">24.08.2018</td>
            </tr>
        </tbody>
        <tbody class="lh-copy">
            <tr>
                <td class="pv3 pr3 bb b--black-20"><img src="./assets/D Miles.png" alt="D Miles"></td>
                <td class="pv3 pr3 bb b--black-20">Kr 15,80</td>
                <td class="pv3 pr3 bb b--black-20">5 øre</td>
                <td class="pv3 pr3 bb b--black-20">24.08.2018</td>
            </tr>
        </tbody>
        <tbody class="lh-copy">
            <tr>
                <td class="pv3 pr3 bb b--black-20"><img src="./assets/95 Miles Plus.png" alt="95 Miles"></td>
                <td class="pv3 pr3 bb b--black-20">Kr 18,01</td>
                <td class="pv3 pr3 bb b--black-20">5 øre</td>
                <td class="pv3 pr3 bb b--black-20">24.08.2018</td>
            </tr>
        </tbody>
        <tbody class="lh-copy">
            <tr>
                <td class="pv3 pr3 bb b--black-20"><img src="./assets/D Miles Plus.png" alt="D Miles"></td>
                <td class="pv3 pr3 bb b--black-20">Kr 16,79</td>
                <td class="pv3 pr3 bb b--black-20">5 øre</td>
                <td class="pv3 pr3 bb b--black-20">24.08.2018</td>
            </tr>
        </tbody>
    </table>
</div>

我尝试过快速使用此代码:

let titles = try doc.getElementsByClass("pv3 pr3 bb b--black-20").array()

但是当我尝试将其打印出来时,我没有得到回报。有人有任何解决方案或想法吗?

1 个答案:

答案 0 :(得分:1)

要选择至少属于多个类之一的元素,请用逗号分隔这些类:

let tds: [Element] = try doc.select(".pv3, .pr3, .bb, .b--black-20")

使用它来选择第二个td

let doc: Document = try SwiftSoup.parse(html)
let td: Element = try doc.select("tbody tr td").array()[1]
let text: String = try td.text()

选择器“ tbody tr td”在td内的tr内查找所有tbody。然后我们知道第二个td是我们想要的那个。因此,我们将结果转换为数组,然后使用下标[1]在该数组中选择第二个元素。

如果您确定只想要html文档中的第二个td,则可以缩短选择器:

let td: Element = try doc.select("td").array()[1]

如果要获取表中所有第二个td,其文本以“ Kr”开头:

let tds: [Element] = try doc.select("tr td").array().filter { try $0.text().starts(with: "Kr ")}
let labels: [String] = try tds.map {try $0.text()}

如果要这些td的文本,但不带“ Kr”:

let tds: [Element] = try doc.select("td").array().filter { try $0.text().starts(with: "Kr ")}
let titlesWithoutKr: [String] = try tds.map {try String($0.text().dropFirst(3))}

这是最终代码:

do {
    let html: String =  """
                        <div class="overflow-auto">
                            <table class="w-100 tl mb4 mt3 f6" cellspacing="0">
                                <thead>
                                    <tr>
                                        <th class="fw6 bb b--black-20 tl pb3 pr3 bg-white tl">Kvalitet</th>
                                        <th class="fw6 bb b--black-20 tl pb3 pr3 bg-white tl">Pris inkl. mva.</th>
                                        <th class="fw6 bb b--black-20 tl pb3 pr3 bg-white tl">Endring</th>
                                        <th class="fw6 bb b--black-20 tl pb3 pr3 bg-white tl">Gjeldene fra</th>
                                    </tr>
                                </thead>
                                <tbody class="lh-copy">
                                    <tr>
                                        <td class="pv3 pr3 bb b--black-20"><img src="./assets/95 Miles.png" alt="95 Miles"></td>
                                        <td class="pv3 pr3 bb b--black-20">Kr 17,02</td>
                                        <td class="pv3 pr3 bb b--black-20">5 øre</td>
                                        <td class="pv3 pr3 bb b--black-20">24.08.2018</td>
                                    </tr>
                                </tbody>
                                <tbody class="lh-copy">
                                    <tr>
                                        <td class="pv3 pr3 bb b--black-20"><img src="./assets/D Miles.png" alt="D Miles"></td>
                                        <td class="pv3 pr3 bb b--black-20">Kr 15,80</td>
                                        <td class="pv3 pr3 bb b--black-20">5 øre</td>
                                        <td class="pv3 pr3 bb b--black-20">24.08.2018</td>
                                    </tr>
                                </tbody>
                                <tbody class="lh-copy">
                                    <tr>
                                        <td class="pv3 pr3 bb b--black-20"><img src="./assets/95 Miles Plus.png" alt="95 Miles"></td>
                                        <td class="pv3 pr3 bb b--black-20">Kr 18,01</td>
                                        <td class="pv3 pr3 bb b--black-20">5 øre</td>
                                        <td class="pv3 pr3 bb b--black-20">24.08.2018</td>
                                    </tr>
                                </tbody>
                                <tbody class="lh-copy">
                                    <tr>
                                        <td class="pv3 pr3 bb b--black-20"><img src="./assets/D Miles Plus.png" alt="D Miles"></td>
                                        <td class="pv3 pr3 bb b--black-20">Kr 16,79</td>
                                        <td class="pv3 pr3 bb b--black-20">5 øre</td>
                                        <td class="pv3 pr3 bb b--black-20">24.08.2018</td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                        """
    let doc: Document = try SwiftSoup.parse(html)
    let tds: [Element] = try doc.select("td").array().filter { try $0.text().starts(with: "Kr ")}
    let titlesWithoutKr: [String] = try tds.map {try String($0.text().dropFirst(3))}
    print(titlesWithoutKr)
} catch Exception.Error( _, let message) {
    print(message)
} catch {
    print("error")
}

它会打印["17,02", "15,80", "18,01", "16,79"]

有关如何使用SwiftSoup的更多文档,请查看here