4

I want to code an app which shows you the data of a specific HTML-Page. How can read the text e.g. in a div and turn that text into a String? How can I search something on that page or look in the row of a table?

2 Answers2

2

You need mind what is the step you need to complete this task.

First need load html page data After find what do you need with parser.

In the first step I found this code

let url = NSURL(string: "http://www.stackoverflow.com")

let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
     println(NSString(data: data, encoding: NSUTF8StringEncoding))
}

task.resume()

Example from this link How to make an HTTP request in Swift?

Look this example, you do a request and return a data, this data need be String. After received data you need parse to findo what do you want.

Second step you need tools to execute, the simple way is do with string methods like slipt. If you need something more smart I recommend Regular Expression

Simple example for split (swift 1.2)

var fullName = "First Last"
var fullNameArr = split(fullName) {$0 == " "}
var firstName: String = fullNameArr[0]
var lastName: String? = fullNameArr.count > 1 ? fullNameArr[1] : nil

example (swift 2.0)

let fullName = "First Last"
let fullNameArr = split(fullName.characters){$0 == " "}.map(String.init)

fullNameArr[0] // First
fullNameArr[1] // Last

Examples from Swift: Split a String into an array

This link is good about Regular Expression, mind it is a powerfull tool to parse, with this you can search anything in your html.

NSRegularExpression Tutorial: Getting Started

I hope helped you with this explanation.

Community
  • 1
  • 1
ViTUu
  • 1,204
  • 11
  • 21
  • Thanks for your answer! I have still a few questions. Let's start with the first one. I have already heard the word parser but can you explain me quickly what it is and how I should implement it. I have tried parsing. Xcode says that it doesn't know "tfhpple" how can I fix it? –  Sep 14 '15 at 18:13
  • 1
    Parser, well... parser is when you need extract some information from som data, example all `html` pages has `` and ``, imagine a website has a `
    `, and you have an app need load price from a html page. For it you can use Regular Expression for search content between `
    `, and show in you app the price of something.. it is parse something.. What is `tfhpple` can say more about it? Some example when it appear!?
    – ViTUu Sep 14 '15 at 23:01
  • I also looked at some other tutorials and they`ve integrated a tfhpple file –  Oct 04 '15 at 07:25
0

Updated syntax (swift 5) with a few modifications to get html data:

let url = NSURL(string: "https://www.stackoverflow.com")
    let task = URLSession.shared.dataTask(with: url! as URL) {(data, response, error) in
        if data != nil { // otherwise will crash for if url is empty
            let dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)!
            print("url data:",dataString)
        } else {print("url data not found")}
    }
task.resume()
Markv07
  • 196
  • 1
  • 8