是否可以提取Webview中加载的URL的源代码? [英] Is it possible to extract the source code of a URL loaded in webview?

查看:217
本文介绍了是否可以提取Webview中加载的URL的源代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这听起来很简单,我知道我将被告知仅获取url会话的内容.但是我特别想知道源代码是否可以从浏览器的最终呈现(后javascript/JSON格式)中获取

This sounds straight forward and I know I'll be told to just get the contents of a url session. But I specifically want to know if source code can be derived from a browser's final rendering (post-javascript/JSON formatting)

我在获取帖子已处理的内容时遇到问题.当我使用swift/objective-c或Java下载URL的内容时,内容字符串具有基于Json搜索首选项的搜索结果的占位符.但是,在浏览器中,右键单击并选择查看源代码"将显示完整的结果页面.我需要一种以编程方式获取此后js处理的内容的方法.

I'm having a problem getting post is processed content. When I download the contents of the url using either swift/objective-c or Java, the content string has placeholders for the search results based on Json search prefs. When in a browser, however, the full results page is visible when right clicking and selecting "view source." I need a way to programmatically get this post-js processed content.

我已经在webView中加载了代码.这是我的代码:

I've loaded the code in webView. Here is my code:

func loadAddressURL() {
    let url = "https://www.adomain.com/apath"
    let URL = NSURL(string: url)!
    let request = NSURLRequest(URL: URL)
    webView.mainFrame.loadRequest(request)
}

推荐答案

使用WKWebView,它是Apple的

Use a WKWebView, which is the Apple's recommended class to embed web view for new code. Then you can execute Javascript through it to get the post-render HTML:

// self.webView is a WKWebView

// In viewDidLoad() or applicationDidFinishLaunching()
self.webView.navigationDelegate = self

func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
    webView.evaluateJavaScript("document.getElementsByTagName('html')[0].innerHTML") { innerHTML, error in
        print(innerHTML!)
    }
}

这篇关于是否可以提取Webview中加载的URL的源代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆