Closed XAlandK closed 4 years ago
Swift do not download data from website but manipolate only. What is the code do you use?
Inviato da iPhone
Il giorno 13 mar 2020, alle ore 23:44, Aland Kawa notifications@github.com ha scritto:
SwiftSoup is very slow for getting data from a webpage. Any solution?
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.
Swift do not download data from website but manipolate only. What is the code do you use? Inviato da iPhone …
I use this to get strings from cells of a table in a webpage, but it takes 2-3 minutes to get it.
func cellData(rowNum: Int, colNum: Int) -> String {
let url = URL(string: "https://www.worldometers.info/coronavirus/")
let tr: [Element]
let td: Elements
var returnString: String? = nil
do {
let html = try String.init(contentsOf: url!)
let doc: Document = try SwiftSoup.parse(html)
//let p: Element = try doc.select("p").first()!
//print(try p.text())
//let h1: [Element] = try doc.select("span").array()
//label.text = "Cases: \(try h1[4].text())"
tr = try doc.select("tr").array()
td = try tr[rowNum].getAllElements()
returnString = try td[colNum].text()
//print(try tr[1].text())
} catch Exception.Error(type: let type, Message: let message) {
print(type)
print(message)
} catch {
print("")
}
return returnString!
}
You are calling first line each time, your slow code depend on it, your sre downloading html too time without an apparent reason. Try to store html in a variable and download it in viewDidAppear or in pullToRefresh action.
let url = URL(string: "https://www.worldometers.info/coronavirus/")
let html = try String.init(contentsOf: url!)
//Optionally also this lines, there no reason initialise same html
let doc: Document = try SwiftSoup.parse(html)
let tr: [Element] = try doc.select("tr").array()
func cellData(tr: [Element],rowNum: Int, colNum: Int) -> String {
let td: Elements
var returnString: String? = nil
do {
td = try tr[rowNum].getAllElements()
returnString = try td[colNum].text()
} catch Exception.Error(type: let type, Message: let message) {
print(type)
print(message)
} catch {
print("")
}
return returnString!
}
PS: code not tested
SwiftSoup is very slow for getting data from a webpage. Any solution?