2

我需要从带有 POST 请求的网站返回的字符串中提取数据;我正在使用 SwiftSoup 库解析数据。我使用 CSS 选择器选择了列表项:

let iconsList: Element = try doc.select("ul.icons-list").first()!

它返回这样的html:

<ul class="icons-list"> 
   <li><strong>Label 1:</strong> Value 1 (Some text) </li> 
   <li><strong>Label 2:</strong> Value 2</li> 
   <li><strong>Label 3:</strong> Value 3</li> 
   <li><strong>Label 4:</strong> Value 4 </li> 
   <li><strong>Label 5:</strong> Value 5</li> 
</ul>

现在我需要提取标签和值并存储在数组或单独的变量中。我已经尝试过如下所示的正则表达式(没有用,可能是错误的正则表达式):

let result = "This <strong>Needs to be removed</strong> is my string"
let regex = try! NSRegularExpression(pattern: "<strong>(.*)</strong>", options: .caseInsensitive)
var newStr = regex.stringByReplacingMatches(in: result, options: [], range: NSRange(0..<str.utf16.count), withTemplate: "")
print(newStr)

并且还尝试了 SwiftSoup 选择器,例如:

var labelFirst = try doc.select("ul.icons-list li:nth-child(1)")

但它也返回 HTML 结果。所以,我需要在这两种情况下使用正则表达式。如何才能做到这一点?

另一个问题: 当我使用 SwiftSoup ".select" 选择器选择图标列表类时。如果有异常,那么我该如何处理呢?目前,我有这个代码,但它不工作。如果我想在这个块中处理多个 try 块怎么办?

do{
      let doc: Document = try SwiftSoup.parse(responseString!)
      let iconsList: Element = try doc.select("ul.icons-list").first()!
      print(iconsList)
  }catch Exception.Error( _, let message){
      print("icons list not found "+message)
  }catch{
      print("error")
  }
4

1 回答 1

1

我能够弄清楚自己。以下是我的做法:

var res = "<ul class=\"icons-list\"><li><strong>Label 1:</strong> Value 1 (Some text) </li></ul>"

extension String {
  func capturedGroups(withRegex pattern: String) -> [String] {
    var results = [String]()

    var regex: NSRegularExpression
    do {
        regex = try NSRegularExpression(pattern: pattern, options: [])
    } catch {
        return results
    }

    let matches = regex.matches(in: self, options: [], range: NSRange(location:0, length: self.characters.count))

    guard let match = matches.first else { return results }

    let lastRangeIndex = match.numberOfRanges - 1
    guard lastRangeIndex >= 1 else { return results }

    for i in 1...lastRangeIndex {
        let capturedGroupIndex = match.rangeAt(i)
        let matchedString = (self as NSString).substring(with: capturedGroupIndex)
        results.append(matchedString)
    }

    return results
  }
}

let label1 = res.capturedGroups(withRegex: "<strong>(.*)</strong>")
let value1 = res.capturedGroups(withRegex: "</strong>(.*)</li>")

print("\(label1[0]): \(value1[0])")
//Output: Label 1:  Value 1 (Some text) 

如果有人给我更好的方法或改进我的功能,我仍然会很感激!

于 2017-06-10T11:12:24.187 回答