I am a newbie on Swift and Swift-HTML-Parser. I am using Swift-HTML-Parser from : https://github.com/tid-kijyun/Swift-HTML-Parser
I need some help for below question.
let myURLString = "http://MytestingWebsite.com/MyAds.html"
let myURL = NSURL(string: myURLString)
var error: NSError?
let myHTMLString = NSString(contentsOfURL: myURL, encoding: NSUTF8StringEncoding, error: &error)
if let error = error {
//for below question 2
println("Error : \(error)")
} else {
// code
}
1) How to get the Src of the Image if there is a) only one image b) a collection of images?
< image Src="....."/>
2) What to use to show message when there is an error?
Adding the the required files as per the above link for Swift-HTML-Parser :
1. Add a Bridging file. click Project and goto Building Setting
Use search: type in Bridging
Double click on the result : Objective-C Bridging Header
and Click (+) at the Top to add.
Error msg show: When adding file name with (-) Like Swift-HTML-Parser-Bridging-Header.h
So, I add SwiftHTMLParserBridgingHeader
2) Copy the File Swift-HTML-Parser-Bridging-Header.h and rename it as SwiftHTMLParserBridgingHeader.h in the project file.
3) Copy HTMLParser.Swift and HTMLNode.swift
4) Have added the reference Libxml2.dylib
When compile, (2) and (3) have red dot. Am I doing something wrong?
This have have a emum for each node that you can search in the HTML
public enum HTMLNodeType : String {
case HTMLUnkownNode = ""
case HTMLHrefNode = "href"
case HTMLTextNode = "text"
case HTMLCodeNode = "code"
case HTMLSpanNode = "span"
case HTMLPNode = "p"
case HTMLLiNode = "li"
case HTMLUiNode = "ui"
case HTMLImageNode = "image"
case HTMLOlNode = "ol"
case HTMLStrongNode = "strong"
case HTMLPreNode = "pre"
case HTMLBlockQuoteNode = "blockquote"
}
From the example they have in the website:
var err : NSError?
//myHTMLString is the value you retrive from the website
var parser = HTMLParser(html: myHTMLString, error: &err)
if err != nil {
//This will log the error and exit the app
//You probably should display an alert to the user
println(err)
exit(1)
}
var bodyNode = parser.body
if let inputNodes = bodyNode?.findChildTags(HTMLNodeType.HTMLImageNode) {
for node in inputNodes {
println(node.contents)
//this should display the address where the Image is
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With