Why can't I receive value from this https request?

Does anyone know what could be wrong with this? Each time I test in simulator the request I am just returned the value "Please Enter a Name"

It's like the https request is not occurring at all. Appreciate any help...!


import UIKit


class AgeLookUpViewController: UIViewController {

@IBOutlet weak var celebrityTextField: UITextField!

@IBOutlet weak var resultLabel: UILabel!

@IBAction func getAge(_ sender: Any) {

if let url = URL(string: "https://en.wikipedia.org/wiki/" + celebrityTextField.text!.replacingOccurrences(of: " ", with: "_")) {

let request = NSMutableURLRequest(url: url)

let task = URLSession.shared.dataTask(with: request as URLRequest) {

data, response, error in

var message = ""

if error != nil {

print(error!)

} else {

if let unwrappedData = data {

let dataString = NSString(data: unwrappedData, encoding: String.Encoding.utf8.rawValue)

var stringSeparator = "<span class=\"noprint ForceAgeToShow\">(age&#160;"

if let contentArray = dataString?.components(separatedBy: stringSeparator) {

if contentArray.count > 1 {

stringSeparator = ")</span>"

let newContentArray = contentArray[1].components(separatedBy: stringSeparator)

if newContentArray.count > 1 {

message = newContentArray[1]

print(message)

}

}

}

}

}

if message == "" {

message = "Please Enter a Name"

}

DispatchQueue.main.sync(execute: {

self.resultLabel.text = message

})

}

task.resume()

} else {

resultLabel.text = "Age is unavailable at this time. Please try again later."

}

}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

self.view.endEditing(true)

}


override func viewDidLoad() {

super.viewDidLoad()


// Do any additional setup after loading the view.

}

You’re trying to parse HTML to extract data, a process known as web scraping. See this post for some background on this.

Looking at your code it seems to fetch the page successfully and then fails trying to parse the HTML. I didn’t look at it to closely, but stepping through the code I see that

contentArray
has only one element, meaning that
stringSeparator
isn’t being found.

You can fix that specific problem, but that’s only papering over the cracks. Web scraping is hard, and my recommendation is that you try to find an approach that doesn’t require it, that is, find a source for the information you need that’s intended to be computer readable.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

You should edit your code with the formatter tool, skip useless blank lines, to make it readable.


Test after adding a lot of print statements (I included some) and report exactly what you get with a name as John Lennon in the celebrityTextField


Here it is.


class AgeLookUpViewController: UIViewController {
  
    @IBOutlet weak var celebrityTextField: UITextField!
  
    @IBOutlet weak var resultLabel: UILabel!
  
    @IBAction func getAge(_ sender: Any) {
      
        print("Celebrity", celebrityTextField.text!)
        if let url = URL(string: "https://en.wikipedia.org/wiki/" + celebrityTextField.text!.replacingOccurrences(of: " ", with: "_")) {
            print("Celebrity url", "https://en.wikipedia.org/wiki/" + celebrityTextField.text!.replacingOccurrences(of: " ", with: "_"))
          
            let request = NSMutableURLRequest(url: url)
          
            let task = URLSession.shared.dataTask(with: request as URLRequest) {
                data, response, error in
                var message = ""
                if error != nil {
                    print("dataTask error", error!)
                } else {
                    if let unwrappedData = data {
                        let dataString = NSString(data: unwrappedData, encoding: String.Encoding.utf8.rawValue)
                        print("dataString", dataString)
                        var stringSeparator = "(age "
                        print("dataString", stringSeparator)

                        if let contentArray = dataString?.components(separatedBy: stringSeparator) {
                            print("contentArray", contentArray)

                            if contentArray.count > 1 {
                                stringSeparator = ")"
                                let newContentArray = contentArray[1].components(separatedBy: stringSeparator)
                                print("newContentArray", newContentArray)

                                if newContentArray.count > 1 {
                                    message = newContentArray[1]
                                    print(message)
                                }
                            }       // if contentArray.count
                        }       // if let contentArray
                    }       // if let unwrappedData
                }       // else
              
                if message == "" {
                    message = "Please Enter a Name"
                }
              
                DispatchQueue.main.sync(execute: {
                    self.resultLabel.text = message
                })
              
            }
          
            task.resume()
          
        } else {
            resultLabel.text = "Age is unavailable at this time. Please try again later."
        }
      
    }
  
  
  
    override func touchesBegan(_ touches: Set, with event: UIEvent?) {
        self.view.endEditing(true)
    }
  
  
    override func viewDidLoad() {
        super.viewDidLoad()
      
        // Do any additional setup after loading the view.
    }
}
Why can't I receive value from this https request?
 
 
Q