网易页面展示
获取页面文字获取方法解析
主要代码如下
// *加载第一个页面
first = [[UIWebView alloc] initWithFrame:self.view.frame];
first.delegate = self;
first.tag = 100;
NSMutableURLRequest *requestceshi =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://nstool.netease.com/"]];
[first loadRequest:requestceshi];
// *以下是webview的两个代理方法,通过tag值来区分,同时获取我们需要的页面地址
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if (webView.tag == 100) {
requestString = [[request URL] absoluteString];
NSLog(@"打印所有的请求 == %@",requestString);
if (![requestString isEqualToString:@"http://nstool.netease.com/"]) {
NSLog(@"requestString == %@",requestString);
second = [[UIWebView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height - 20)];
second.delegate = self;
second.tag = 1000;
[second loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:requestString]]];
}
}
return YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
if (webView.tag == 1000) {
if (![requestString isEqualToString:@"http://nstool.netease.com/"]) {
NSString *doc = @"document.documentElement.innerHTML";
NSString *doc2 = @"document.body.innerText";
NSString *doc3 = @"document.documentElement.textContent";
NSString *doc4 = @"document.body.innerHTML";
NSString *html1 = [webView stringByEvaluatingJavaScriptFromString:doc];
NSString *html2 = [webView stringByEvaluatingJavaScriptFromString:doc2];
NSString *html3 = [webView stringByEvaluatingJavaScriptFromString:doc3];
NSString *html4 = [webView stringByEvaluatingJavaScriptFromString:doc4];
NSLog(@"lHtml1 == %@",html1);
NSLog(@"lHtml2 == %@",html2);
NSLog(@"lHtml3 == %@",html3);
NSLog(@"lHtml3 == %lu",(unsigned long)html3.length);
NSLog(@"lHtml4 == %@",html4);
NSArray *array = [html4 componentsSeparatedByString:@"<br>"];
NSLog(@"打印lHtml4数组 == %@",array[2]);
NSString *string = array[2];
NSString *ceshi = [string substringWithRange:NSMakeRange(10, 16)];
NSLog(@"打印lHtml4数组截取 == %@",ceshi);
}
}
}
小结