htmlswiftparsingalamofireswiftsoup

Swift - Parsing a Web Page


For the project I am currently working on, I need to download and parse the HTML for getting specific date and times.

I created a dummy project for that testing and here is the code:

import UIKit
import Alamofire
import SwiftSoup

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let diyanetURL = "https://namazvakitleri.diyanet.gov.tr/tr-TR/"

        let params = ["ulkeId" : 2, "ilId" : 500,"ilceId" : 9146]
        Alamofire.request(diyanetURL, method: .post, parameters: params, encoding: URLEncoding.default).validate(contentType: ["application/x-www-form-urlencoded"]).response { (response) in

            if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
                do {
                    let html: String = utf8Text
                    let doc: Document = try SwiftSoup.parse(html)
                    let bla = try doc.getElementsByAttribute("tbody")
                    for bl in bla {
                        print(bl)
                    }

                } catch let error {
                    print(error.localizedDescription)
                }

            }
        }
    }
}

I want to get that part of the website but I couldn't:

<tbody>

                                        <tr>

                                            <td>04.12.2017</td>

                                            <td>06:16</td>

                                            <td>07:47</td>

                                            <td>12:46</td>

                                            <td>15:10</td>

                                            <td>17:32</td>

                                            <td>18:56</td>

                                        </tr>

                                        <tr>

                                            <td>05.12.2017</td>

                                            <td>06:17</td>

                                            <td>07:48</td>

                                            <td>12:46</td>

                                            <td>15:09</td>

                                            <td>17:31</td>

                                            <td>18:56</td>

                                        </tr>

How can I parse the web page and get these dates and times?

Thanks in advance.


Solution

  • I found the solution:

    import UIKit
    import Alamofire
    import SwiftSoup
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            let diyanetURL = "https://namazvakitleri.diyanet.gov.tr/tr-TR/8648"
    
    //        let params = ["ulkeId" : 2, "ilId" : 500,"ilceId" : 9146]
            Alamofire.request(diyanetURL, method: .post, parameters: nil, encoding: URLEncoding.default).validate(contentType: ["application/x-www-form-urlencoded"]).response { (response) in
    
                if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
                    do {
                        let html: String = utf8Text
                        let doc: Document = try SwiftSoup.parse(html)
                        for row in try! doc.select("tr") {
                            print("------------------")
                            for col in try! row.select("td") {
                                print(try col.text())
                            }
                        }
    
                    } catch let error {
                        print(error.localizedDescription)
                    }
    
                }
            }
        }
    }