0

我正在尝试将数据从网站解析到我自己的应用程序。我设法获取 html 并使用 AlamoFire 打印 html,但我不知道如何从该 html 文件中获取特定数据。

在这里我得到了 HTML,我正在尝试获取交换数据

func scrapeExchangeRate() -> Void {
    Alamofire.request("https://yatirim.akbank.com/tr-tr/doviz/sayfalar/default.aspx").responseString {
        response in
        print("\(response.result.isSuccess)")
        if let html = response.result.value {
            self.parseHTML(html: html)
        }
    }
}

func parseHTML(html: String) -> Void {
    print("\(html)")
}

该语句按预期打印出整个 HTML 文件,但我试图从 var 中提取数据,如下所示

var dovizHesaplama = '{"EUR":{"USD":{"ask":1.14065,"bid":1.14058,"time":"2019-01-16T22:21:45.3497922+03:00"},"GBP ":{"ask":0.88690615037710907,"bid":0.88705864053507533,"time":"2019-01-16T22:06:44+03:00"},"XAU":{"ask":0.00088175725295877431,"bid" :0.00088189402550006575,"time":"2019-01-16T22:06:44+03:00"},"AUD":{"ask":1.5884277955716475,"bid":1.5889941487879631,"time":"2019-01- 16T22:06:44+03:00"},"CAD":{"ask":1.51113312,"bid":1.510812268,"time":"2019-01-16T22:06:44+03:00"}, "CHF":{"ask":1.128901305,"bid":1.12860391,"time":"2019-01-16T22:06:44+03:00"},"CNY":{"ask":7.7056610749999992,"bid":7.70233674,"time":"2019-01-16T22:06:44+03:00"},"DKK":{"ask" :7.4654401849999994,"bid":7.463613346,"time":"2019-01-16T22:06:44+03:00"},"ETH":{"ask":0.0091456863373957659,"bid":0.0091480590311196659,"time" :"2019-01-16T22:06:44+03:00"},"NOK":{"ask":9.74137913,"bid":9.738043924,"time":"2019-01-16T22:06:44+ 03:00"},"NZD":{"ask":1.680975905,"bid":1.68064463,"time":"2019-01-16T22:06:44+03:00"},"RUB":{"问":75.636729629999991,"投标":75.603231241999993,"时间":"2019-01-16T22:06:44+03:00"},"SAR":{"询问“:4.2788062799999995”,“投标”:4.277859348,“时间”:“2019-01-16T22:06:44+03:00”},“瑞典克朗”:{“询问”:10.261743659999999,“投标”:10.25894681,时间":"2019-01-16T22:06:44+03:00"},"TRL":{"ask":6.09837116,"bid":6.094803288,"time":"2019-01-16T22:06: 44+03:00"}

我对从这里开始的美元汇率感兴趣:

我特别想提取数据,例如某些特定货币的汇率出价和要价与美元的关系。如何选择这些特定值?

"USD":{"ask":1.0101010101010102,"bid":1.0105092966855296,"time":"2019-01-16T21:05:25.7963428+03:00"},"GBP":{"ask":0.78619318968011376,"bid":0.78663342416746818,"time":"2019-01-16T20:50:16+03:00"},"XAU":{"ask":0.00078045880988148267,"bid":0.00078094926132039845,"time":"2019-01-16T20:50:16+03:00"},"AUD":{"ask":1.4070218834113528,"bid":1.4087680143392298,"time":"2019-01-16T20:50:16+03:00"},"CAD":{"ask":1.3381818181818181,"bid":1.338419563459984,"time":"2019-01-16T20:50:16+03:00"},"CNY":{"ask":6.8226262626262635,"bid":6.82407033144705,"time":"2019-01-16T20:50:16+03:00"},"DKK":{"ask":6.6151515151515161,"bid":6.6170169765561839,"time":"2019-01-16T20:50:16+03:00"},"ETH":{"ask":0.0080872779031305863,"bid":0.00809184254232487,"time":"2019-01-16T20:50:16+03:00"},"NOK":{"ask":8.6312121212121227,"bid":8.6322756669361365,"time":"2019-01-16T20:50:16+03:00"},"NZD":{"ask":1.4894949494949494,"bid":1.4896928051738076,"time":"2019-01-16T20:50:16+03:00"},"RUB":{"ask":67.052121212121222,"bid":67.059013742926439,"time":"2019-01-16T20:50:16+03:00"},"SAR":{"ask":3.7890909090909091,"bid":3.790016168148747,"time":"2019-01-16T20:50:16+03:00"},"SEK":{"ask":9.0835353535353534,"bid":9.0852869846402591,"time":"2019-01-16T20:50:16+03:00"},"TRL":{"ask":5.4033333333333342,"bid":5.4027890056588523,"time":"2019-01-16T20:50:16+03:00"},"XAG":{"ask":0.064750064750064756,"bid":0.0649010466721599,"time":"2019-01-16T20:50:16+03:00"},"XPD":{"ask":0.000743858997658927,"bid":0.0007465456764177438,"time":"2019-01-16T20:50:16+03:00"},"XPT":{"ask":0.0012522016836102078,"bid":0.001253951426656652,"time":"2019-01-16T20:50:16+03:00"}}
4

1 回答 1

0

就像 gavsta707 评论的那样,您可以使用正则表达式在 HTML 内容中查找 var var dovizHesaplama = '(.+)'

然后使用JSONDecoder. 请注意,您需要指定日期格式,即YYYY-MM-dd'T'HH:mm:ss.SSSSSSSZZZZZ.

一些执行此操作的代码可能看起来像

import Foundation

struct ExchangeRates: Codable {

    struct Rate: Codable {
        let ask: Double
        let bid: Double
        let time: Date
    }

    struct OtherRates: Codable {
        let EUR: Rate
        let GBP: Rate
        let XAU: Rate
        let AUD: Rate
        let CAD: Rate
        let CHF: Rate
        let CNY: Rate
        let DKK: Rate
        let ETH: Rate
        let NOK: Rate
        let NZD: Rate
        let RUB: Rate
        let SAR: Rate
        let SEK: Rate
        let TRL: Rate
        let XAG: Rate
        let XPD: Rate
        let XPT: Rate
    }

    let USD: OtherRates
}

let jsonPattern = try NSRegularExpression(pattern: "var dovizHesaplama = '(.+)'")
let decoder = JSONDecoder()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "YYYY-MM-dd'T'HH:mm:ss.SSSSSSSZZZZZ"
decoder.dateDecodingStrategy = .formatted(dateFormatter)

let data = try Data(contentsOf: URL(string: "https://yatirim.akbank.com/tr-tr/doviz/sayfalar/default.aspx")!)
if let string = String(data: data, encoding: .utf8) as NSString? {
    if let match = jsonPattern.firstMatch(in: string as String, range: NSRange(location: 0, length: string.length)) {
        let json = string.substring(with: match.range(at: 1))
        let rates = try decoder.decode(ExchangeRates.self, from: json.data(using: .utf8)!)
        print(rates)
    }
}
于 2019-01-16T20:02:16.737 回答