是否可以只获取通过URL获取的HTML字符串的一部分?
以下示例代码:
let myURLString = "https://myUrl.something"
guard let myURL = NSURL(string: myURLString) else {
print("Error: \(myURLString) doesn't seem to be a valid URL")
return
}
do {
let myHTMLString = try String(contentsOf: myURL as URL)
let htmlString = String(myHTMLString)
print("HTML: \(myHTMLString)")
} catch let error as NSError {
print("Error: \(error)")
}
我想将标记<h3 class="post-title">
中的内容添加到</h3>
。
我知道我应该使用正则表达式,但我真的不知道如何以正确的方式设置它。我试过这样的事情:
let myURLString = "https://www.fvgtech.it/category/podcast/"
guard let myURL = NSURL(string: myURLString) else {
print("Error: \(myURLString) doesn't seem to be a valid URL")
return
}
do {
let myHTMLString = try String(contentsOf: myURL as URL)
let htmlString = String(myHTMLString)
if let match = htmlString.range(of: "(<h3.+)", options: .regularExpression) {
print("Found",htmlString.substring(with: match))
}
print("HTML: \(myHTMLString)")
} catch let error as NSError {
print("Error: \(error)")
}
但它只打印<h3 class="post-title">
而不是中间的打印。提前致谢!
答案 0 :(得分:2)
我们需要在start String和end String之间搜索所有子字符串。参见String of Extension
let myURLString = "https://www.fvgtech.it/category/podcast/"
guard let myURL = NSURL(string: myURLString) else {
print("Error: \(myURLString) doesn't seem to be a valid URL")
return
}
do {
let myHTMLString = try String(contentsOf: myURL as URL)
let htmlString = String(myHTMLString)
print(htmlString.allStringsBetween("<h3 class=\"post-title\">", andString: "</h3>"))
} catch let error as NSError {
print("Error: \(error)")
}
字符串扩展
extension String{
func allStringsBetween(start: String, end: String) -> [Any] {
var strings = [Any]()
var startRange: NSRange = (self as NSString).range(of: start)
while true {
if startRange.location != NSNotFound {
var targetRange = NSRange()
targetRange.location = startRange.location + startRange.length
targetRange.length = self.count - targetRange.location
let endRange: NSRange = (self as NSString).range(of: end, options: [], range: targetRange)
if endRange.location != NSNotFound {
targetRange.length = endRange.location - targetRange.location
strings.append((self as NSString).substring(with: targetRange))
var restOfString = NSRange()
restOfString.location = endRange.location + endRange.length
restOfString.length = self.count - restOfString.location
startRange = (self as NSString).range(of: start, options: [], range: restOfString)
}
else {
break
}
}
else {
break
}
}
return strings
}
}