关键在于 :
@"document.getElementsByTagName('html')[0].innerHTML"
@"document.body.innerHTML"
目的: 这样可以很灵活的得到你想得东西。
1. 设置UIWebView代理
<UIWebViewDelegate>
2. 使用下面方法:
- (void)webViewDidFinishLoad:(UIWebView *)webView;
3. 代码:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//获取所有源代码:
NSString *jsToGetHTMLSource =@"document.getElementsByTagName('html')[0].innerHTML";
NSString *HTMLSource = [self.webViewstringByEvaluatingJavaScriptFromString:jsToGetHTMLSource];
NSLog(@"%@",HTMLSource);
//获取页面的代码body的内容
NSString *JsToGetHTMLSource = @"document.body.innerHTML";
NSString *pageSource = [webView stringByEvaluatingJavaScriptFromString:JsToGetHTMLSource];
NSLog(@"pagesource:%@", pageSource);
}