首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Swift解析html表

Swift解析html表
EN

Stack Overflow用户
提问于 2015-05-15 04:05:10
回答 2查看 2.2K关注 0票数 2

我正在尝试将网页中的这些信息带入iOS应用程序。

HTML代码:

代码语言:javascript
复制
<table border="0" cellpadding="3" cellspacing="0" width="85%"><tr><td width="100%" colspan="3" bgcolor="#C9C9E7"><b>Update as of 3:57:00 PM (CDT) Thu., Apr. 16, 2015</b><br></td></tr><tr>
<td width="50%" bgcolor="#FFFFFF">Production Line 1</td>
<td width="35%" bgcolor="#FFFFFF">9:00 minutes  (10 min)&nbsp;</td>
<td width="15%" bgcolor="#FFFFFF">No delay</td>
</tr><tr>
<td width="50%" bgcolor="#FFFFFF"><b>Production Line 2</b></td>
<td width="35%" bgcolor="#FFFFFF"><b>7:57 minutes  </b><b>(4 min)&nbsp;</b></td>
<td width="15%" bgcolor="#FFFFFF"><b>+3:57</b></td>
</tr><tr>
<td width="50%" bgcolor="#FFFFFF"><b>Production Line 3</b></td>
<td width="35%" bgcolor="#FFFFFF"><b>10:35 minutes  </b><b>(8 min)&nbsp;</b></td>
<td width="15%" bgcolor="#FFFFFF"><b>+2:35</b></td>
</tr></table>

这是我目前尝试编写的代码。我已经标记了我认为是//问题所在的行。我只得到可选的nil值。我正在使用Swift-HTML-Parser来帮助实现这一点。我只做了一个更改,那就是为table添加一个部分。

代码语言:javascript
复制
import UIKit
extension String {
var html2String:String {
    return NSAttributedString(data: dataUsingEncoding(NSUTF8StringEncoding)!, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:NSUTF8StringEncoding], documentAttributes: nil, error: nil)!.string
   }
}

class mainViewController: UIViewController {

@IBOutlet var textView: UITextView!


override func viewDidLoad() {
    super.viewDidLoad()
    updateTrafficInfo()

    let singleFingerTap = UITapGestureRecognizer(target: self, action: "handleSingleTap:")
    self.textView.addGestureRecognizer(singleFingerTap)
}

// MARK: gestutre recognizer
func handleSingleTap(recognizer: UITapGestureRecognizer) {
    updateTrafficInfo()
}

func updateTrafficInfo(){
    var request = HTTPTask()
    var err: NSError?
    request.GET("http://localhost/productiontimes.html", parameters: nil, success: {(response: HTTPResponse) in
        if let data = response.responseObject as? NSData {
            let rawHTML = NSString(data: data, encoding: NSUTF8StringEncoding) as! String
            let option = CInt(HTML_PARSE_NOERROR.value | HTML_PARSE_RECOVER.value)

            var parser = HTMLParser(html: rawHTML, encoding: NSUTF8StringEncoding, option: option, error: &err)
            if err != nil {
                println(err)
            }
            var bodyNode = parser.table
            if let inputNodes = bodyNode?.xpath("//tr") {
                for node in inputNodes {
                    dispatch_sync(dispatch_get_main_queue(), {
                    self.textView.text = node.xpath("//td")?[0].contents  //issue
                    println(node.xpath("//td")?[0].contents)
                    });
                }
            }


            dispatch_sync(dispatch_get_main_queue(), {
                self.textView.text = rawHTML.html2String


            });

        }
        },failure: {(error: NSError, response: HTTPResponse?) in
            println("error: \(error)")
    })
}



override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

目标是将输出格式设置为如下所示。

代码语言:javascript
复制
Production Line  -      Time  -                     Delay
Production Line 1 -     9:00 minutes  (10 min) -    No delay
Production Line 2 -     7:57 minutes  (4 min) -     +3:57
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-05-15 05:22:09

编辑/更新: Swift 4.x

代码语言:javascript
复制
extension Data {
    var html2AttributedString: NSAttributedString? {
        return try? NSAttributedString(data: self, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)

    }
    var html2String: String {
        return html2AttributedString?.string ?? ""
    }
}

代码语言:javascript
复制
let data = Data("""
<table border="0" cellpadding="3" cellspacing="0" width="85%"><tr><td width="100%" colspan="3" bgcolor="#C9C9E7"><b>Update as of 3:57:00 PM (CDT) Thu., Apr. 16, 2015</b><br></td></tr><tr>
<td width="50%" bgcolor="#FFFFFF">Production Line 1</td>
<td width="35%" bgcolor="#FFFFFF">9:00 minutes  (10 min)&nbsp;</td>
<td width="15%" bgcolor="#FFFFFF">No delay</td>
</tr><tr>
<td width="50%" bgcolor="#FFFFFF"><b>Production Line 2</b></td>
<td width="35%" bgcolor="#FFFFFF"><b>7:57 minutes  </b><b>(4 min)&nbsp;</b></td>
<td width="15%" bgcolor="#FFFFFF"><b>+3:57</b></td>
</tr><tr>
<td width="50%" bgcolor="#FFFFFF"><b>Production Line 3</b></td>
<td width="35%" bgcolor="#FFFFFF"><b>10:35 minutes  </b><b>(8 min)&nbsp;</b></td>
<td width="15%" bgcolor="#FFFFFF"><b>+2:35</b></td>
</tr></table><table border="0" cellpadding="3" cellspacing="0" width="85%"><tr><td width="100%" colspan="3" bgcolor="#C9C9E7"><b>Update as of 3:57:00 PM (CDT) Thu., Apr. 16, 2015</b><br></td></tr><tr>
<td width="50%" bgcolor="#FFFFFF">Production Line 1</td>
<td width="35%" bgcolor="#FFFFFF">9:00 minutes  (10 min)&nbsp;</td>
<td width="15%" bgcolor="#FFFFFF">No delay</td>
</tr><tr>
<td width="50%" bgcolor="#FFFFFF"><b>Production Line 2</b></td>
<td width="35%" bgcolor="#FFFFFF"><b>7:57 minutes  </b><b>(4 min)&nbsp;</b></td>
<td width="15%" bgcolor="#FFFFFF"><b>+3:57</b></td>
</tr><tr>
<td width="50%" bgcolor="#FFFFFF"><b>Production Line 3</b></td>
<td width="35%" bgcolor="#FFFFFF"><b>10:35 minutes  </b><b>(8 min)&nbsp;</b></td>
<td width="15%" bgcolor="#FFFFFF"><b>+2:35</b></td>
</tr></table>
""".utf8)

代码语言:javascript
复制
let output = data.html2String
let components = output.components(separatedBy: .newlines)
for index in stride(from: 1, to: 9, by: 3) {
    let line = components[index]
    let time = components[index+1]
    let delay = components[index+2]
    print( line + " - " + time + " - " + delay )
}
票数 3
EN

Stack Overflow用户

发布于 2015-05-15 05:41:27

您在XPath中遇到了一些问题,请参阅以下代码:

代码语言:javascript
复制
let html = "<table border='0' cellpadding='3' cellspacing='0' width='85%'><tr><td width='100%' colspan='3' bgcolor='#C9C9E7'><b>Update as of 3:57:00 PM (CDT) Thu., Apr. 16, 2015</b><br></td></tr><tr>" +
        "<td width='50%' bgcolor='#FFFFFF'><b>Production Line 1</b></td>" +
        "<td width='35%' bgcolor='#FFFFFF'><b>9:00 minutes</b><b>(10 min)&nbsp;</b></td>" +
        "<td width='15%' bgcolor='#FFFFFF'><b>No delay</b></td>" +
        "</tr><tr>" +
        "<td width='50%' bgcolor='#FFFFFF'><b>Production Line 2</b></td>" +
        "<td width='35%' bgcolor='#FFFFFF'><b>7:57 minutes  </b><b>(4 min)&nbsp;</b></td>" +
        "<td width='15%' bgcolor='#FFFFFF'><b>+3:57</b></td>" +
        "</tr><tr>" +
        "<td width='50%' bgcolor='#FFFFFF'><b>Production Line 3</b></td>" +
        "<td width='35%' bgcolor='#FFFFFF'><b>10:35 minutes  </b><b>(8 min)&nbsp;</b></td>" +
        "<td width='15%' bgcolor='#FFFFFF'><b>+2:35</b></td>" +
    "</tr></table>"



    var err : NSError?
    var parser  = HTMLParser(html: html, error: &err)
    if err != nil {
        println(err)
        exit(1)
    }        

    var table = parser.html

    // avoid the first <td> tag 
    if let inputNodes = table?.xpath("//td[position() > 1]/b") {

        println("Production Line  -      Time  -                     Delay")            
        for (index, node) in enumerate(inputNodes) {
            if index % 4 == 0 {
                println("\n")
            }
            print(node.contents + "-    ")
        }
    }

输出如下:

代码语言:javascript
复制
Production Line  -      Time  -                     Delay

Production Line 1-    9:00 minutes-    (10 min) -    No delay-    

Production Line 2-    7:57 minutes  -    (4 min) -    +3:57-    

Production Line 3-    10:35 minutes  -    (8 min) -    +2:35- 

您可以根据需要对输出进行个性化设置。我希望这对你有帮助。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30246349

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档