0

对于我目前正在进行的项目,我需要下载并解析 HTML 以获得特定的日期和时间。

我为该测试创建了一个虚拟项目,代码如下:

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)
                }

            }
        }
    }
}

我想获得网站的那部分,但我不能:

<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>

如何解析网页并获取这些日期和时间?

提前致谢。

4

2 回答 2

1

我找到了解决方案:

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)
                }

            }
        }
    }
}
于 2017-12-04T10:30:37.943 回答
0

为什么不直接使用 API:https ://ahmetimamoglu.com.tr/ahmeti-namaz-vakitleri-api-get-metodu-ile 。

于 2017-12-04T10:21:45.863 回答