开始使用SwiftSoup库进行解析。我使用自定义类和由类定义的数组:
names: [AddClass] = []
class AddClass: NSObject {
var name500: String
var date500: String
var ima: String
var prices: String
var sales: String
var display_price: String
init (name500: String, date500: String, ima: String, prices: String, sales: String, display_price: String) {
self.name500 = name500
self.date500 = date500
self.ima = ima
self.prices = prices
self.sales = sales
self.display_price = display_price
}
}
解析如下:
var Title: [Element] = []
var pp: Int = 1
var myURL: URL!
do {
while pp < 6{
myURL = URL(string: "https://..../\(pp)")
pp += 1
let html = try! String(contentsOf: myURL!, encoding: .utf8)
let doc: Document = try SwiftSoup.parse(html)
Title += try doc.select(".item-details-title")
}
} catch Exception.Error(let type, let message) {
print(message)
} catch {
print("error")
}
问题是,如何将数据分配给在Title中获得的name500类变量?
标题依次包含我需要的文字:
Title.map{$0.text()}
以前以不同的解析方式分配的数据,其工作方式类似于
names.map {names in
primerTitle.map {
names.name500 += $0.text()
}
}