Is it possible to take just a part of the HTML string that I have obtained passing the URL?
Example code below:
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)")
}
I want to take what's inside the tag <h3 class="post-title"> to </h3>.
I know that I should use the regular expressions but I don't really know how to set it in the right way. I tried something like this:
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)")
}
But it's printing just <h3 class="post-title"> and not what's in the middle. Thanks in advance!
String(htmlString.filter { !" \n\t\r".contains($0) }).range(of: "<h3.*?>(.+)((.*)+(.+))+</h3>", options: .regularExpression)But I do not advise you to use this method because it will take a very long time to better use this library github.com/scinfu/SwiftSoup