Swift,使用SwiftSoup到达多个类以解析HTML文件

时间:2018-10-26 13:28:07

标签: javascript swift swiftsoup

我正在解析HTML网站。 (使用swiftsoup cocoapods)我通过getElementByClass方法访问了带有类的数据。

struct OddResponse {

let odds: [Odd]

init(_ innerHTML: Any?) throws {
guard let htmlString = innerHTML as? String else { throw
    HTMLError.badInnerHTML }

let doc = try SwiftSoup.parse(htmlString)
let leagues = try doc.getElementsByClass("league").array()
let times = try doc.getElementsByClass("time").array()
let hometeams = try doc.getElementsByClass("homeTeam").array()
let awayteams = try doc.getElementsByClass("awayTeam").array()
let dropprimaries = try doc.getElementsByClass("drop primary").array()

let odd1highdrops = try doc.getElementsByClass("odds-dropping drop-high").array()
let odd2highdrops = try doc.getElementsByClass("odds-dropping drop-high").array()

    var odds = [Odd]()
    for i in 0..<times.count {
        let league = try leagues[i].text()
        let time = try times[i].text()

        let odd1highdrop = try odd1highdrops[i].text()
        let odd2highdrop = try odd2highdrops[i].text()

        print(odd1highdrop)
        print(odd2highdrop)

        let odd = Odd(league: league, time: time, odd1highdrop: odd1highdrop, odd2highdrop: odd2highdrop)
        odds.append(odd)
    }
    self.odds = odds
}

但就我而言,HTML正在发生变化。因此,“奇数下降高点”类名可以每分钟更新一次。如何迅速达到孩子班级或家长班级。有人知道吗?

1 个答案:

答案 0 :(得分:0)

从问题中提取:

这对我来说很完美。

struct OddResponse {

let odds: [Odd]

init(_ innerHTML: Any?) throws {
    guard let htmlString = innerHTML as? String else { throw
        HTMLError.badInnerHTML }


    var odds = [Odd]()
    let doc = try SwiftSoup.parse(htmlString)
    if let body = try doc.getElementsByClass("tablesorter tablesorter-default hasStickyHeaders").last()?.getElementsByTag("tbody").first() {
        for tr in body.children() {

            let league = try tr.getElementsByClass("league").first()!.text()
            let time = try tr.getElementsByClass("time").first()!.text()

            var otherOdd1dropping = ""
            var odd1dropping = ""
            var oddDroppingType = 0

            if let node = try tr.getElementsByClass("odds odds-1 odds-dropping").first() {
                odd1dropping = try node.text()
            }else if let node = try tr.getElementsByClass("odds odds-1 odds-dropping drop-medium").first() {
                otherOdd1dropping = try node.text()
                oddDroppingType = 1
            }else{
                otherOdd1dropping = try tr.getElementsByClass("odds odds-1 odds-dropping drop-high").first()!.text()
                oddDroppingType = 2
            }

            let odd = Odd(league: league, time: time, odd1dropping: odd1dropping, otherOdd1dropping: otherOdd1dropping, oddDroppingType: oddDroppingType)
            odds.append(odd)
        }
    }
    self.odds = odds
}