Get HTML from query url (index.php?[arguments] ) Swift 3 ios

185 Views Asked by At

I'm working on Ios and Swift 3 and I want to parse HTML Code. Until now Hpple did the trick for me using this code:

func parse_Html_Text(url:String)
{
    let data = NSData(contentsOf: URL(string: url)!)
    let doc = TFHpple(htmlData: data! as Data!)
}

But when I'm trying to get html code from a query url (like this one: link), then my app crashes and I get Thread1: EXC_BAD_INSTRUCTION error in this line:

let doc = TFHpple(htmlData: data! as Data!)

I also tried Alamofire's request method but I didnt manage to make it work

I'm stucked two days with this so any help will be appreciated.

Thank you in advance!

1

There are 1 best solutions below

1
axel On

I noticed that you use Swift 3. Probably the library expects an NSData object but I would suggest to write your function like the following:

func parse_Html_Text(url: String) {
    if let safeUrl = URL(string: url) {
        let data = try? Data(contentsOf: safeUrl)
        let doc = TFHpple(htmlData: data as? NSData)
    }
}

In this case keep in mind that we are not handling possible error from the data setter.