如何通过 Swift3 将 HTML 字符串分隔为数组或字典?
Posted
技术标签:
【中文标题】如何通过 Swift3 将 HTML 字符串分隔为数组或字典?【英文标题】:How to separate HTML string into array or dictionary by Swift3? 【发布时间】:2018-03-07 09:10:39 【问题描述】:我从 API 中得到了这样的 html 字符串:
let a: String = "<a href="https://www.google.com.tw">https://www.google.com.tw </a>"
let b: String = "<a href="myAppName://app/user/aa3b77411825b88b318d77gg">@Tim </a>Hello Tim"
let c: String = "<a href="myAppName://app/user/aa3b77411825b88b318d77gg">@Tim </a><a href="https://www.google.com.tw">https://www.google.com.tw </a>"
let splitedArray1: [String] = a.componentsSeparatedByString("?????") //splited string which is the best
let splitedArray2: [String] = b.componentsSeparatedByString("?????") //splited string which is the best
let splitedArray3: [String] = c.componentsSeparatedByString("?????") //splited string which is the best
我想将链接与它们分开并获取如下数据
print(splitedArray1) //["https://www.google.com.tw","https://www.google.com.tw"]
print(splitedArray2) //["myAppName://app/user/aa3b77411825b88b318d77gg","@Tim ","Hello Tim"]
print(splitedArray3) //["myAppName://app/user/aa3b77411825b88b318d77gg","@Tim ","https://www.google.com.tw","https://www.google.com.tw "]
【问题讨论】:
【参考方案1】:可能的解决方案:使用NSAttributedString
然后枚举NSLinkAttributeName
,如果没有,则表示没有链接标签,所以你只保留“字符串”,否则,你添加链接,然后字符串。
在 Playground 中快速编写:
let a: String = "<a href=\"https://www.google.com.tw\">https://www.google.com.tw </a>"
let b: String = "<a href=\"myAppName://app/user/aa3b77411825b88b318d77gg\">@Tim </a>Hello Tim"
let c: String = "<a href=\"myAppName://app/user/aa3b77411825b88b318d77gg\">@Tim </a><a href=\"https://www.google.com.tw\">https://www.google.com.tw </a>"
let values:[String] = [a, b, c]
for aHTMLString in values
let attributedString = try! NSAttributedString.init(data: aHTMLString.data(using: .utf8)!,
options: [.documentType: NSAttributedString.DocumentType.html],
documentAttributes: nil)
var retValues = [String]()
attributedString.enumerateAttribute(.link,
in: NSRange(location: 0, length: attributedString.string.count),
options: [],
using: (attribute, range, pointerStop) in
if let attribute = attribute as? URL
retValues.append(attribute.absoluteString)
let subString = (attributedString.string as NSString).substring(with: range)
retValues.append(subString)
)
print("*** retValues: \(retValues)")
let targetResult1 = ["https://www.google.com.tw","https://www.google.com.tw"]
let targetResult2 = ["myAppName://app/user/aa3b77411825b88b318d77gg","@Tim ","Hello Tim"]
let targetResult3 = ["myAppName://app/user/aa3b77411825b88b318d77gg","@Tim ","https://www.google.com.tw","https://www.google.com.tw "]
print("targetResult1: \(targetResult1)")
print("targetResult2: \(targetResult2)")
print("targetResult3: \(targetResult3)")
输出:
*** retValues: ["https://www.google.com.tw/", "https://www.google.com.tw "]
*** retValues: ["myappname://app/user/aa3b77411825b88b318d77gg", "@Tim ", "Hello Tim"]
*** retValues: ["myappname://app/user/aa3b77411825b88b318d77gg", "@Tim ", "https://www.google.com.tw/", "https://www.google.com.tw "]
targetResult1: ["https://www.google.com.tw", "https://www.google.com.tw"]
targetResult2: ["myAppName://app/user/aa3b77411825b88b318d77gg", "@Tim ", "Hello Tim"]
targetResult3: ["myAppName://app/user/aa3b77411825b88b318d77gg", "@Tim ", "https://www.google.com.tw", "https://www.google.com.tw "]
有一些小的差异,我复制了你的“目标”(splitArray),它在最后一个中缺少一个空格,我的代码倾向于在链接上添加最后一个“/”。
【讨论】:
【参考方案2】:我创建了这个扩展来获取 url。
extension String
func getUrl() -> String?
let rss = self.split (char) -> Bool in
return char == ">"
if let final = rss.last?.split(separator: "<"), let first = final.first
return String(first)
return nil
var hrefUrl: String
let matchString = "=\""
let arrComponents = self.components(separatedBy: matchString)
if let first = arrComponents.last, let str = first.split(separator: "\"").first
return String(str)
return ""
用法:
let a: String = "<a href=\"https://www.google.com.tw\">https://www.google.com.tw </a>"
a.getUrl() //output: https://www.google.com.tw
//or
a.hrefUrl //output: https://www.google.com.tw
【讨论】:
这是swift4?我使用 swift3 playgound 进行测试,“让 rss = self.split .......”出现错误。字符串没有拆分功能。【参考方案3】:没有库的简单解决方案 - 只需使用 String.replaceOccurences(of:... 将奇怪的字符串替换为 href、a 到拆分参数(如“|”),然后使用 componentsSeparatedByString(“|”) 来获取您的组件.
【讨论】:
【参考方案4】:使用正则表达式提取 URL。下面我写了sn-p的代码。
let text = "<a href=\"https://www.google.com\">"
let regex = try! NSRegularExpression(pattern: "<a[^>]+href=\"(.*?)\"[^>]*>")
let range = NSMakeRange(0, text.characters.count)
let matches = regex.matches(in: text, range: range)
for match in matches
let strURL = (text as NSString).substring(with: match.rangeAt(1))
print(strURL)
【讨论】:
但我的文字是 google.com.tw\">google.com.tw 。我想打印数组而不是字符串以上是关于如何通过 Swift3 将 HTML 字符串分隔为数组或字典?的主要内容,如果未能解决你的问题,请参考以下文章