WKWebview和UIWebview获取html中的标签

2019-03-07  本文已影响0人  关羽007

举个例子:

//js的标签内容

<meta name="attach_title" content="">

<meta name="attach_url" content="">

//UIWebView获取方法

    NSString *js = @"document.getElementsByName(\"attach_title\")[0].content";

    NSString *attach_title = [self.webView stringByEvaluatingJavaScriptFromString:js];

    js = @"document.getElementsByName(\"attach_url\")[0].content";

    NSString *attach_url = [self.webView stringByEvaluatingJavaScriptFromString:js];

//WKWebView的获取方法

- (void)getWKwebViewCenterDic {

    [self.wkWebView evaluateJavaScript:@"document.getElementsByName(\"attach_title\")[0].content" completionHandler:^(id result, NSError * _Nullable error) {

        NSString *attach_title = @"";

        if ([result isKindOfClass:[NSString class]]) {

            attach_title = result;

        }

    }];

    [self.wkWebView evaluateJavaScript:@"document.getElementsByName(\"attach_url\")[0].content" completionHandler:^(id result, NSError * _Nullable error) {

        NSString *attach_url = @"";

        if ([result isKindOfClass:[NSString class]]) {

            attach_url = result;

        }

    }];

}

//获取所有的html

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

//获取网页title

NSString *htmlTitle = @"document.title";

上一篇 下一篇

猜你喜欢

热点阅读