Reading HTML content from a UIWebView

前端 未结 10 2455
感动是毒
感动是毒 2020-11-22 13:47

Is it possible to read the raw HTML content of a web page that has been loaded into a UIWebView?

If not, is there another way to pull raw HTML content f

相关标签:
10条回答
  • 2020-11-22 14:26

    In Swift v3:

    let doc = webView.stringByEvaluatingJavaScript(from: "document.documentElement.outerHTML")
    
    0 讨论(0)
  • 2020-11-22 14:29

    UIWebView

    get HTML from UIWebView`

    let content = uiWebView.stringByEvaluatingJavaScript(from: "document.body.innerHTML")
    

    set HTML into UIWebView

    //Do not forget to extend a class from `UIWebViewDelegate` and nil the delegate
    
    func someFunction() {
    
        let uiWebView = UIWebView()
        uiWebView.loadHTMLString("<html><body></body></html>", baseURL: nil)
        uiWebView.delegate = self as? UIWebViewDelegate
    }
    
    func webViewDidFinishLoad(_ webView: UIWebView) {
        //ready to be processed
    }
    

    [get/set HTML from WKWebView]

    0 讨论(0)
  • 2020-11-22 14:35

    if you want to extract the contents of an already-loaded UIWebView, -stringByEvaluatingJavaScriptFromString. For example:

    NSString  *html = [webView stringByEvaluatingJavaScriptFromString: @"document.body.innerHTML"];
    
    0 讨论(0)
  • 2020-11-22 14:37

    To get the whole HTML raw data (with <head> and <body>):

    NSString *html = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.outerHTML"];
    
    0 讨论(0)
提交回复
热议问题