Swift Code Fragments
Revision as of 11:51, 14 January 2019 by PeterHarding (talk | contribs) (Created page with " From: https://stackoverflow.com/questions/31080818/what-is-the-best-practice-to-parse-html-in-swift <pre> let file = "file.txt" if let dirs : [String] = NSSearchPathForDire...")
From: https://stackoverflow.com/questions/31080818/what-is-the-best-practice-to-parse-html-in-swift
let file = "file.txt" if let dirs : [String] = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true) as? [String] { let dir = dirs[0] //documents directory let path = dir.stringByAppendingPathComponent(file); let html = String(contentsOfFile: path, encoding: NSUTF8StringEncoding, error: nil)
Edit:
import Foundation let html = "theHtmlYouWannaParse" 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("b") { for node in inputNodes { println(node.contents) } } if let inputNodes = bodyNode?.findChildTags("a") { for node in inputNodes { println(node.getAttributeNamed("href")) //<- Here you would get your files link } } shareimprove this answer