Answer the question
In order to leave comments, you need to log in
Does HTML parsing with Swift-HTML-Parser work for real websites?
I'm new to programming in general and Swift in particular, however I've taken a couple of courses and wanted to make a very simple application that would get news from a website and add it to a tableView.
At the moment I'm getting an error:
Optional(Error Domain=HTMLParserdomain Code=1 "The operation couldn't be completed. (HTMLParserdomain error 1.)")
func perFormConnectionToGrabUrlContent(# url:String)-> NSString {
let url = NSURL(string: url)
let request = NSURLRequest(URL: url!)
var htmlContentTemp:NSString = ""
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {(response, data, error) in
htmlContentTemp = NSString(data: data, encoding: NSUTF8StringEncoding)!
println(htmlContentTemp)
}
return htmlContentTemp
}
let html = perFormConnectionToGrabUrlContent(url: "http://www.google.com")
println(html)
var err : NSError?
var parser = HTMLParser(html: html, error: &err)
if err != nil {
println(err)
exit(1)
}
var bodyNode = parser.body
if let inputNodes = bodyNode?.findChildTags("a") {
for node in inputNodes {
println(node.contents)
}
}
if let inputNodes = bodyNode?.findChildTags("a") {
for node in inputNodes {
println(node.contents)
println(node.getAttributeNamed("href"))
}
}
Answer the question
In order to leave comments, you need to log in
var htmlContentTemp:NSString = ""
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {(response, data, error) in
htmlContentTemp = NSString(data: data, encoding: NSUTF8StringEncoding)!
println(htmlContentTemp)
}
return htmlContentTemp
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question