我想从一个IQ网站上提问和回答。我已经采取的问题和答案,当我点击答案时,他会转到下一个问题。这很好用。我解析了这个网站:http://test.mensa.no/ 但最终有一个问题:要显示答案(IQ),我想点击按钮Finish。在这里,我需要使用方法发布以计算得分数和一些公式来显示结果。
我的例子:
var request = URLRequest(url: URL(string: self.IQLogicLink)!)
request.httpMethod = "POST"
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
let dict = parameters(count: self.IQLogicArray.count)
request.httpBody = dict.queryString.data(using: .ascii, allowLossyConversion: false)
let task = URLSession.shared.dataTask(with: request) {(data, response, error) in
if let content = data {
let string = String(data: content, encoding: .utf8)!
let page = try! Kanna.HTML(html: string, encoding: .utf8)
let result = page.css("div[id^='result']").first!
let block = result.css("p").first!.text!
self.resultLabel.text = "\(block)"
} else {
}
}
task.resume()
以下是parameters
功能:
func parameters(count: Int) -> [String : Any] {
var dict = ["submitted": 1, "curq": count, "_ifx": 1, "_nvh":[""]] as [String : Any]
for i in 0..<33 {
dict["q\(i)"] = self.answers[i]
}
return dict
}