我正在尝试解析一些html类并在textview中显示。 Here's a screenshot of the webpage with the html classes.我需要“ t-gurmukhi”课程。
我已经成功打印了p.tag-links,但是t-gurmukhi给了我零。
我正在使用SwiftSoup库尝试执行此操作,并使用Alamofire来获取链接。到目前为止,这是我的代码:
import UIKit
import Alamofire
import SwiftSoup
class HtmlDecodeViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
downloadHTML()
}
func downloadHTML() {
let getURL = "https://www.someurl.com"
Alamofire.request(getURL, method: .post, parameters: nil, encoding: URLEncoding.default).validate(contentType: ["application/x-www-form-urlencoded"]).response { (response) in
if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
do {
let html: String = utf8Text
let doc: Document = try SwiftSoup.parse(html)
for infoTags in try! doc.select("p.tag") {
let someText = try infoTags.text()
self.hukamInfo.text = someText
}
let els: Elements = try doc.getElementsByClass("t-tag")
let el: Element? = els.first()//get first element
print(try "\(String(describing: el?.text()))")
} catch let error {
// an error occurred
print(error.localizedDescription)
}
}
}
}