iOS获取网页的标题,内容,节点值

Posted BearsG

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS获取网页的标题,内容,节点值相关的知识,希望对你有一定的参考价值。

最近一直在做混合开发,经常遇到一些静态网页,需获取网页的标题等操作,可以用下面方法

还有,注意webview的代理方法

 - (void)webViewDidFinishLoad:(UIWebView *)webView 

网页前进和回退都会调用

- (void)viewDidLoad 

[super viewDidLoad];

NSString *strurl=@"http://baidu.com";

UIWebView *web = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

web.delegate = self;

[web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:strurl]]];

[self.view addSubview:web];

// Do any additional setup after loading the view, typically from a nib.


- (void)webViewDidFinishLoad:(UIWebView *)webView 

UIWebView *web = webView;

//获取所有的html

NSString *allHtml = @"document.documentElement.innerHTML";

//获取网页title

NSString *htmlTitle = @"document.title";

//获取网页的一个值

NSString *htmlNum = @"document.getElementById('title').innerText";

//获取到得网页内容

NSString *allHtmlInfo = [web stringByEvaluatingjavascriptFromString:allHtml];

NSLog(@"%@",allHtmlInfo);

NSString *titleHtmlInfo = [web stringByEvaluatingJavaScriptFromString:htmlTitle];

NSLog(@"%@",titleHtmlInfo);

NSString *numHtmlInfo = [web stringByEvaluatingJavaScriptFromString:htmlNum];

NSLog(@"%@",numHtmlInfo);

参考自简书作者:流浪在简书

以上是关于iOS获取网页的标题,内容,节点值的主要内容,如果未能解决你的问题,请参考以下文章