gpt4 book ai didi

javascript - Swift,使用 SwiftSoup 访问多个类来解析 HTML 文件

转载 作者:行者123 更新时间:2023-11-30 11:03:31 26 4
gpt4 key购买 nike

我正在解析一个 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 正在发生变化。因此“odds-dropping drop-high”类别名称可以每分钟更新一次。我怎样才能快速联系到 child 的类(class)或家长。有人知道吗?

最佳答案

摘自问题:

这对我来说非常适合。

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
}

关于javascript - Swift,使用 SwiftSoup 访问多个类来解析 HTML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53009851/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com