[英]Unable to extract substring from larger mutli-line string in Swift
我提取了一些 HTML 代碼並將其存儲在一個名為“html”的字符串中。 我想從這個 HTML 代碼中提取星數。 查看 html,我認為最簡單的方法是在 html 字符串和 go 中查找“星星”一詞。 所以我寫了這段代碼:
var s = "stars"
if (html.contains(s)) {
print("Found stars")
}
但是,由於某種原因,這不起作用,並且不會在 xcode 的右下部分打印“Found Stars”。 我究竟做錯了什么?
這是存儲在 html 字符串變量中的 html 代碼的一部分
</h2> <div class="a-row a-size-small a-color-secondary"><span class="a-size-small" dir="auto">by </span><span class="a-size-small" dir="auto">John Bolton</span></div> </div> <div class="a-section a-spacing-none a-spacing-top-mini"> <div class="a-row a-size-small"> <span aria-label="3.1 out of 5 stars"> <i class="a-icon a-icon-star-small a-star-small-3 aok-align-bottom"><span class="a-icon-alt">3.1 out of 5 stars</span></i> </span>
這是我用來提取 HTML 的代碼:
var html = ""
let scheme = "https"
let host = "www.amazon.com"
let path = "/s"
let k = ocrText
let i = "stripbooks"
let kItem = URLQueryItem(name: "k", value: k)
let iItem = URLQueryItem(name: "i", value: i)
var urlComponents = URLComponents()
urlComponents.scheme = scheme
urlComponents.host = host
urlComponents.path = path
urlComponents.queryItems = [kItem, iItem]
guard let url = urlComponents.url else { return }
print(url)
//URL(string: "https://www.amazon.com" + "/s?k=" + text + "&i=stripbooks")!
URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data else {
print(error ?? "")
return
}
html = String(data: data, encoding: .utf8)!
我認為可能是 .contains 方法只檢查了 HTML 的第一行,但我可能錯了。
關於評級的更多代碼:
URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data else {
print(error ?? "")
return
}
html = String(data: data, encoding: .utf8)!
let pattern = #"(\d.\d) out of 5 stars"#
if let range = html.range(of: pattern, options: .regularExpression) {
let rating = html[range].prefix(3)
ocrText = ocrText + rating
print("testing", ocrText)
}
}.resume()
print("LATER:", ocrText)
DispatchQueue.main.async {
self.ocrTextView.text = ocrText
self.scanButton.isEnabled = true
}
創建html
String
,如下所示,
let html = """
</h2>
<div class="a-row a-size-small a-color-secondary"><span class="a-size-small" dir="auto">by </span><span class="a-size-small" dir="auto">John Bolton</span></div>
</div>
<div class="a-section a-spacing-none a-spacing-top-mini">
<div class="a-row a-size-small">
<span aria-label="3.1 out of 5 stars">
<i class="a-icon a-icon-star-small a-star-small-3 aok-align-bottom"><span class="a-icon-alt">3.1 out of 5 stars</span></i>
</span>
"""
現在下面的代碼將在您的控制台中打印找到的星星。
var s = "stars"
if (html.contains(s)) {
print("Found stars")
}
確保您使用 escaping 特殊字符(即,將“替換為 \”)存儲字符串,如下所示:
var str = "</h2> <div class=\"a-row a-size-small a-color-secondary\"><span class=\"a-size-small\" dir=\"auto\">by </span><span class=\"a-size-small\" dir=\"auto\">John Bolton</span></div></div><div class=\"a-section a-spacing-none a-spacing-top-mini\"><div class=\"a-row a-size-small\"><span aria-label=\"3.1 out of 5 stars\"><i class=\"a-icon a-icon-star-small a-star-small-3 aok-align-bottom\"><span class=\"a-icon-alt\">3.1 out of 5 stars</span></i></span>"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.