获取加载的 UIWebView 的 HTML 源代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5167254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting the HTML source code of a loaded UIWebView
提问by Wasim A.
How can I get the HTML
source code of a UIWebView
and store it in NSString
?
如何获取 a 的HTML
源代码UIWebView
并将其存储在 中NSString
?
回答by Guillaume
If you want the source code of an already loaded webview, you can get it like this:
如果你想要一个已经加载的 webview 的源代码,你可以这样得到:
NSString *yourHTMLSourceCodeString = [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];
Update:method above works most of the time. But I recently was in a situation where I needed all the code source, not just the body.
更新:上述方法大部分时间都有效。但我最近遇到了一种情况,我需要所有的代码源,而不仅仅是正文。
Here is a better way:
这是一个更好的方法:
NSString *yourHTMLSourceCodeString = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.outerHTML"];
(It won't reload the webview).
(它不会重新加载网络视图)。
回答by Sudhanshu
I am not sure that what exactly you need..........but as per your last comment you will get that html data from google like this...
我不确定你到底需要什么..........但是根据你的最后一条评论,你会像这样从谷歌获得 html 数据......
NSData *companyData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"]];
NSString *responseString = [[NSString alloc] initWithData:companyData encoding:NSUTF8StringEncoding];
self.yourTextView.text = responseString; // response string contains that data
[companyData release];
Good Luck!
祝你好运!