gpt4 book ai didi

html - 从 HTML key-value 获取数据

转载 作者:搜寻专家 更新时间:2023-11-01 05:33:33 24 4
gpt4 key购买 nike

我想从 HTML 中获取键值。目前使用 swift soup 通过索引获取元素。如果网站元素索引发生变化,我将无法获取该值。想要根据键取回值以下示例 HTML:

<table class="Tbl"><tbody><tr><td colspan="4" style="background-color:#D49F3E"><div class="Val" style="text-align:center;color:#540000">Saturday, December 30, 2017</div></td></tr><tr><td style="width:18%"><div class="Key">Sunrise:</div></td><td style="width:32%"><div class="Val">06:30</div></td><td style="width:18%"><div class="Key">Sunset:</div> </td><td style="width:32%"><div class="Val">17:52</div></td></tr>

我现在的代码是

do{
let htmlContent = html
do{
let doc = try SwiftSoup.parse(htmlContent as String)
do{
let element = try doc.select("[class=Val]").array()
do{
let today = try element[0].text()
let Sunrise = try element[1].text()
print(today)
print(Sunrise)
}catch{
}
}catch{
}
}catch{
}
}

请教如何通过 key 获得阀门

编辑-1:试图申请。在代码下方并出现错误。有什么建议吗?

let data = try? Data(contentsOf: url! as URL)
let html = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)!

do{
let html1: String = html as String
let els: Elements = try SwiftSoup.parse(html1).getElementsByClass("Tbl")
for keyVal: Element in els.array(){
let keyValText: String = try keyVal.text()
print(keyValText)
let components = keyValText
.components(separatedBy: .newlines)
.filter{
!$0.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
}
if let index = components.index(where: {$0.contains(find: "Sunrise")}){
let sunriseTest = components[components.index(after: index)] //Error : Thread 1: Fatal error: Index out of range
print("sunriseTest: ", sunriseTest)
}else{

}
}

}catch Exception.Error(let type, let message){
print(message)
}catch{
print("error")
}

最佳答案

您可以将您的 html 转换为字符串,并获取由换行符分隔的组件。要修复索引问题,只需在修剪空格后过滤空行:

struct Sun {
let date: String
let sunset: String
let sunrise: String
init?(string: String) {
guard string.count > 4 else { return nil }
let components = string
.components(separatedBy: .newlines)
.filter {
!$0.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
}
date = components[0]
sunset = components[2]
sunrise = components[4]
}
init?(data: Data) throws {
self.init(string: try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil).string)
}
}

Playground 测试:

let html1 = """
<table class="Tbl"><tbody><tr><td colspan="4" style="background-color:#D49F3E"><div class="Val" style="text-align:center;color:#540000">Saturday, December 30, 2017</div></td></tr><tr><td style="width:18%"><div class="Key">Sunrise:</div></td><td style="width:32%"><div class="Val">06:30</div></td><td style="width:18%"><div class="Key">Sunset:</div> </td><td style="width:32%"><div class="Val">17:52</div></td></tr>
"""
let html2 = """
<table class="Tbl"><tbody><tr><td colspan="4" style="background-color:#D49F3E"><div class="Val" style="text-align:center;color:#540000">Saturday, December 30, 2017</div></td></tr><tr><td style="width:18%"><div class="Key">Sunrise:</div></td><td style="width:32%"><div class="Val">06:30</div></td><td style="width:18%"><div class="Key">&nbsp;</div></td><td style="width:32%"><div class="Val">&nbsp;</div></td><td style="width:18%"><div class="Key">Sunset:</div> </td><td style="width:32%"><div class="Val">17:52</div></td></tr>
"""

do {
if let sun = try Sun(data: Data(html2.utf8)) {
print("Today:", sun.date)
print("Sunset:", sun.sunset)
print("Sunrise:",sun.sunrise)
}
} catch {
print(error)
}

Today: Saturday, December 30, 2017 Sunset: 06:30 Sunrise: 17:52

关于html - 从 HTML key-value 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48036110/

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