【问题标题】:Swift html parsing快速 html 解析
【发布时间】:2018-07-08 04:12:45
【问题描述】:

我正在尝试解析这个网站:http://www.ggjh.co.kr/now/menu.php

使用以下代码:

fileprivate func parseMenuHtml(){

    if let url = URL(string: "http://www.ggjh.co.kr/now/menu.php") {
        do {
            let contents = try String(contentsOf: url)
            print(contents)
        } catch {
            print("error")
            // contents could not be loaded
        }
    } else {
        // the URL was bad!
    }

它不起作用,我也使用了 kanna,但没有起作用。

我不知道出了什么问题 TT。 使用~.com,上面的代码运行良好....php 可能是问题...?

【问题讨论】:

  • * 请按照帮助页面中的说明编辑您的问题,使其成为主题:寻求调试帮助的问题(“为什么此代码不起作用?”)必须包含所需的行为,特定问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:How to create a Minimal, Complete, and Verifiable example 此外,切勿使用 String(contentsOf:) 从远程 URL 下载数据。

标签: php html swift


【解决方案1】:

请看: How To Get HTML source from URL with Swift

斯威夫特 3 和 4

let myURLString = "https://google.com"
guard let myURL = URL(string: myURLString) else {
    print("Error: \(myURLString) doesn't seem to be a valid URL")
    return
}

do {
    let myHTMLString = try String(contentsOf: myURL, encoding: .ascii)
    print("HTML : \(myHTMLString)")
} catch let error {
    print("Error: \(error)")
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-14
    • 2011-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多