Reading HTML content from a UIWebView

2018-12-31 20:09发布

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 from a web page in the iPhone SDK (such as an equivalent of the .NET WebClient::openRead)?

9条回答
心情的温度
2楼-- · 2018-12-31 20:09

(Xcode 5 iOS 7) Universal App example for iOS 7 and Xcode 5. It is an open source project / example located here: Link to SimpleWebView (Project Zip and Source Code Example)

查看更多
不流泪的眼
3楼-- · 2018-12-31 20:10

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

NSString  *html = [webView stringByEvaluatingJavaScriptFromString: @"document.body.innerHTML"];
查看更多
高级女魔头
4楼-- · 2018-12-31 20:18

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

NSString *html = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.outerHTML"];
查看更多
步步皆殇っ
5楼-- · 2018-12-31 20:21

I use a swift extension like this:

extension UIWebView {
    var htmlContent:String? {
        return self.stringByEvaluatingJavaScript(from: "document.documentElement.outerHTML")
    }

}
查看更多
浪荡孟婆
6楼-- · 2018-12-31 20:28

To read:-

NSString *html = [myWebView stringByEvaluatingJavaScriptFromString: @"document.getElementById('your div id').textContent"];
NSLog(html);    

To modify:-

html = [myWebView stringByEvaluatingJavaScriptFromString: @"document.getElementById('your div id').textContent=''"];
查看更多
余欢
7楼-- · 2018-12-31 20:29

you should try this:

document.documentElement.outerHTML
查看更多
登录 后发表回答