-
Notifications
You must be signed in to change notification settings - Fork 159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parsing HTML and then pretty printing it #18
Comments
What do you mean exactly by The code you shared actually does the following things:
So I don't really think it makes sense. |
I guess you only want to |
How might you create the html String? I'm using a separate class that inherits from NSObject to parse down a URL. func httpGet(request: NSURLRequest!, callback: @escaping (String, String?) -> Void) {
var session = URLSession.shared
var task = session.dataTask(with: request as URLRequest){
(data, response, error) -> Void in
if error != nil {
callback("error", error?.localizedDescription)
} else {
var result = String(data: data!, encoding:
String.Encoding(rawValue: String.Encoding.ascii.rawValue))!
callback(result as String, nil)
}
}
task.resume()
} in my ViewController I'm trying to : let html = data
do {
let doc = try HTMLDocument(string: html, encoding: String.Encoding.utf8)
} catch {
} but I get an error for the 'html' variable, 'use of unresolved identifier.' How do I set the html as a string from my initial URLrequest set up in my data service? |
@hvtor you don't need to create the string if you have NSData. It is stated in the README that you can create a document with either a String, an NSData(Data for Swift 3), or [CChar] instance. Actually having a NSData instance is simpler since you don't have to specify the Encoding. let doc = try HTMLDocument(data: data) |
@cezheng Yes. Thank you. :) BTW, great documentation. Just a bit 😴 I guess. |
@hvtor haha, it's true. Any suggestions on improving it? |
No I meant I am 😴. It's great documentation. An example webpage would be great too! Showing how the elements can be mapped over. I'm not too familiar with I'm trying to parse an IMDb list and it's a series of anchor tags. It's not clear to me how you select for specific tags. Walking Dead Stranger Thingsgoes to read the docs again Parent. And then the child tags. |
I'm trying to parse some html text and then pretty print the entire document. I couldn't tell what was the best way to traverse the hierarchy of nodes/elements and wasn't sure how to get the inner html content of a tag. I'm posting here because I think this could improve the documentation for the API.
Is this correct?
The text was updated successfully, but these errors were encountered: