在Swift 4+中使用WKWebView和Regex执行/修改Javascript后获取HTML;
我一直在尝试从一个网站检索一些数据,它看起来像javascript在加载之前/之后执行修改 我想在HTML中找到一个特定的DIV,但它似乎不像我这样做,它只加载HTML的一部分,而不是我需要的部分 这是我的密码:在Swift 4+中使用WKWebView和Regex执行/修改Javascript后获取HTML;,javascript,swift,regex,Javascript,Swift,Regex,我一直在尝试从一个网站检索一些数据,它看起来像javascript在加载之前/之后执行修改 我想在HTML中找到一个特定的DIV,但它似乎不像我这样做,它只加载HTML的一部分,而不是我需要的部分 这是我的密码: let url = URL(string: "https://www.spaceweatherlive.com") let task = URLSession.shared.dataTask(with: url!) { (data, response, error)
let url = URL(string: "https://www.spaceweatherlive.com")
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil{
print(error)
}else{
// let htmlcontent = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
let htmlcontent = String(data: data!, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))
// let regex = try? NSRegularExpression(pattern: "<h4 class=\"text-center\">Kp[0-9]</h4>", options: .caseInsensitive)
let substring = "<h4 class=\"text-center\">Kp[0-9]"
let matched = self.matches(for: substring, in: htmlcontent!)
print(matched)
if htmlcontent!.contains(substring){
print("Substring FOUND!!")
}else{
// print("Nope, printing whole html:")
// print(htmlcontent)
}
// print(htmlcontent)
}
}
task.resume()
谢谢!非常感谢您提供的任何帮助或建议
这是我需要检索的DIV(KP[0-9]值)
我们可以从一个简单的表达式开始,捕捉问题中指标的SVG highchart部分:
(<div id="Kp_gauge".+?<\/div><\/div><\/div><\/div>)
如果我们愿意的话,我们也可以对标题做同样的处理,或者我们可以在标题的顶部编写或添加Kp索引
,这样问题就可以解决了
h3`的标题永远是:
<h3 class="card-header text-center">Kp-index <span class="text-primary" data-toggle="tooltip" title="" data-original-title="The Kp-index is a geomagnetic activity index based on data from magnetometers around the world. The gauge below shows the most recently observed Kp-value from the Planetary K-index of the NOAA SWPC and can be used to make a rough estimate of the current global geomagnetic conditions."><svg class="svg-inline--fa fa-info-circle fa-w-16" aria-hidden="true" focusable="false" data-prefix="fas" data-icon="info-circle" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" data-fa-i2svg=""><path fill="currentColor" d="M256 8C119.043 8 8 119.083 8 256c0 136.997 111.043 248 248 248s248-111.003 248-248C504 119.083 392.957 8 256 8zm0 110c23.196 0 42 18.804 42 42s-18.804 42-42 42-42-18.804-42-42 18.804-42 42-42zm56 254c0 6.627-5.373 12-12 12h-88c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h12v-64h-12c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h64c6.627 0 12 5.373 12 12v100h12c6.627 0 12 5.373 12 12v24z"></path></svg><!-- <i class="fas fa-info-circle" aria-hidden="true"></i> --></span></h3>
Kp索引
这可以简单地添加
const regex=/({
log(`Found match,group${groupIndex}:${match}`);
});
}
这是一个好主意,但特别是在这种情况下,我们需要检索当前小工具值以将其存储到变量中,并在swift中处理它,swift位于正则表达式的div中,这就是为什么Kp[0-9]正则表达式值只定位“KPnumber”值的原因是!没错!有没有可能因为屏幕分辨率而得到H3?正如您在屏幕截图中看到的,我得到了一个H4Well,它可以工作,并且在代码片段中得到了具有当前值的h4!是斯威夫特4号吗?我迫不及待地想试试!这是这里的主要问题。。。WKWebView在不执行javascript或在javascript执行之前加载HTML文档,因此gauge div永远不存在
<div id="Kp_gauge"
<\/div><\/div><\/div><\/div>
<h3 class="card-header text-center">Kp-index <span class="text-primary" data-toggle="tooltip" title="" data-original-title="The Kp-index is a geomagnetic activity index based on data from magnetometers around the world. The gauge below shows the most recently observed Kp-value from the Planetary K-index of the NOAA SWPC and can be used to make a rough estimate of the current global geomagnetic conditions."><svg class="svg-inline--fa fa-info-circle fa-w-16" aria-hidden="true" focusable="false" data-prefix="fas" data-icon="info-circle" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" data-fa-i2svg=""><path fill="currentColor" d="M256 8C119.043 8 8 119.083 8 256c0 136.997 111.043 248 248 248s248-111.003 248-248C504 119.083 392.957 8 256 8zm0 110c23.196 0 42 18.804 42 42s-18.804 42-42 42-42-18.804-42-42 18.804-42 42-42zm56 254c0 6.627-5.373 12-12 12h-88c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h12v-64h-12c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h64c6.627 0 12 5.373 12 12v100h12c6.627 0 12 5.373 12 12v24z"></path></svg><!-- <i class="fas fa-info-circle" aria-hidden="true"></i> --></span></h3>